How to properly compare two Integers in Java?

前端 未结 10 1339
情歌与酒
情歌与酒 2020-11-21 07:06

I know that if you compare a boxed primitive Integer with a constant such as:

Integer a = 4;
if (a < 5)

a will automaticall

10条回答
  •  清酒与你
    2020-11-21 07:41

    Since Java 1.7 you can use Objects.equals:

    java.util.Objects.equals(oneInteger, anotherInteger);
    

    Returns true if the arguments are equal to each other and false otherwise. Consequently, if both arguments are null, true is returned and if exactly one argument is null, false is returned. Otherwise, equality is determined by using the equals method of the first argument.

提交回复
热议问题