Java 实现'comparable'和'comparator'接口:如何检查int变量是否格式正确?

Java 实现'comparable'和'comparator'接口:如何检查int变量是否格式正确?,java,int,comparator,comparable,Java,Int,Comparator,Comparable,我正在学习在我自己的类中实现comparable和comparator接口的概念,我看到了Java教程oracle中关于实现comparable和comparator接口的最佳实践的陈述: 构造函数检查其参数是否为null。这确保了所有 名称对象的格式良好,因此其他方法都不会 曾经抛出过NullPointerException 我正在尝试创建一个Employee类,其中int-id、int-salary、int-age、String name和dateofjoin作为实例变量。我知道诸如int之

我正在学习在我自己的类中实现
comparable
comparator
接口的概念,我看到了Java教程oracle中关于实现
comparable
comparator
接口的最佳实践的陈述:

构造函数检查其参数是否为null。这确保了所有 名称对象的格式良好,因此其他方法都不会 曾经抛出过NullPointerException

我正在尝试创建一个
Employee
类,其中
int-id、int-salary、int-age、String name
dateofjoin
作为实例变量。我知道诸如int之类的基元类型不能等于null,但我应该如何检查int变量的格式是否正确

public class Employee implements Comparable<Employee>{
    public static void main(String[] args) {

    }

    private int id;
    private String name;
    private int salary;
    private int age;
    private Date dateOfJoining;


    //how to check whether int varaibles are well formed or not? 
    public Employee(int id, String name, int salary, int age, Date dateOfJoining){
        this.id = id;
        this.name = name;
        this.salary = salary;
        this.age = age;
        this.dateOfJoining = dateOfJoining;
    }

    @Override
    public int compareTo(Employee o) {
        // to be implemented
    }
}
public类Employee实现了可比较的{
公共静态void main(字符串[]args){
}
私有int-id;
私有字符串名称;
私人薪酬;
私人互联网;
加入日期;
//如何检查int变量的格式是否正确?
公共雇员(整数id、字符串名称、整数工资、整数年龄、加入日期){
this.id=id;
this.name=名称;
这个。薪水=薪水;
这个。年龄=年龄;
this.dateOfJoining=加入日期;
}
@凌驾
公共内部比较(员工o){
//实施
}
}
我发现相关的

构造函数检查其参数是否为null。这确保了所有 名称对象的格式良好,因此其他方法都不会 曾经抛出过NullPointerException


Name
对象有两个字符串成员,必须检查它们是否为null。因为如果其中一个为null
lastName.compareTo(n.lastName)此部分将导致
NullPointerException
。您不需要为int成员创建这样的控件。因为正如您所说,int变量不能是
null

您提到的教程就是教程。特定语句提到:如果您创建了这样一个
Name
对象,其中
firstName
lastName
对象为
null
,那么
compareTo
方法将抛出
NullPointerException
。这可以通过立即检查构造函数参数来防止

更一般地说,您应该确保
compareTo
方法可以不引发任何异常

正如您已经注意到的,从这个意义上讲,
int
变量不可能“格式错误”。它不能为
null
,并且将
int
值与另一个值进行比较不会导致异常

但是,本教程中还列出了其他几点,您应该注意以下几点:

  • 您应该实现
    hashCode
  • 您应该实现
    equals
  • 您应该确保
    compareTo
    的实现与equals
    一致,如中所述
  • 如果可能,将所有字段设置为final,这样类是不可变的

提示:您可以使用该方法方便地比较int值


旁注:请记住,尽管
int
值在技术意义上始终是“格式良好的”,但在构造函数中肯定存在业务约束。在您的示例中,您可能希望为构造函数中的某些字段插入额外的健全性检查:

public Employee(int id, String name, int salary, int age, Date dateOfJoining){
    this.id = id;
    this.name = Objects.requireNonNull(name);
    if (salary <= 0) {
        throw new IllegalArgumentException(
            "The salary should be positive, but is "+salary);
    }
    this.salary = salary;
    if (age <= 0) {
        throw new IllegalArgumentException(
            "The age should be positive, but is "+salary);
    }
    this.age = age;
    this.dateOfJoining = Objects.requireNonNull(dateOfJoining);
}
公共雇员(整数id、字符串名称、整数工资、整数年龄、加入日期){
this.id=id;
this.name=Objects.requirennoull(name);

if(salary)什么会使
int
变量格式不正确?这些将是“业务规则”。对于上下文示例,只有正年龄才有意义(甚至可以限制该范围)。在何时何地增加值限制上有很多灰色区域。@ElliottFrisch在这种情况下,可能是一个负整数,一个不是整数的数字?@dzjustinli3一个
int
-1
仍然是一个
int
;没有任何上下文,我们不知道什么会使
int
无效。