例如,我有这个简单的构造函数:
public Car(String brand, String model, int price){
this.brand=brand;
this.model=model;
this.price=price;
}
所以,我需要创建对象来检查数据库:
new Car (null, null, 15000);
new Car (Opel, null, null);
在第一种情况下我没有问题,但在第二种情况下,不允许整数值为空。
有什么建议吗?
谢谢!
最佳答案
您可以使用一些默认值(例如 -1)来表示 null,或者使用可以为 null 的 Integer
,而不是 int
。
关于java - 整数 null 以检查数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26373578/