How to properly compare two Integers in Java?

前端 未结 10 1365
情歌与酒
情歌与酒 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:38

    Because comparaison method have to be done based on type int (x==y) or class Integer (x.equals(y)) with right operator

    public class Example {
    
        public static void main(String[] args) {
         int[] arr = {-32735, -32735, -32700, -32645, -32645, -32560, -32560};
    
            for(int j=1; j"+arr[j]);
    
    
        Integer[] I_arr = {-32735, -32735, -32700, -32645, -32645, -32560, -32560};
    
            for(int j=1; j"+I_arr[j]);
        }
    }
    

提交回复
热议问题