如何==和equals方法在java中的Integer和Integer []中执行

时间:2017-02-28 08:54:19

标签: java arrays compare

这里我写了简单的java代码。

整数示例

class Question {

        public static void main(String[] args) {
            Integer arr1 = 1;
            Integer arr2 = 1;

            System.out.println("arr1 == arr2 is " + (arr1 == arr2));//this check object reference - this returns true 
            System.out.println("arr1.equals(arr2) is " + arr1.equals(arr2));// this check value - this also return true
        }
    }

整数[]示例

class Question {

      public static void main(String[] args) {
        Integer[] arr1 = {1, 2, 3, 4, 5};
        Integer[] arr2 = {1, 2, 3, 4, 5};

        System.out.println("arr1 == arr2 is " + (arr1 == arr2));// i know this check object reference - but this return false -how it happen ?
        System.out.println("arr1.equals(arr2) is " + arr1.equals(arr2));// i know this check value - but this also return false -how it happen ?
    }
}

对于Integer,它返回true,即可。 但是我无法理解For Integer []它如何返回false。 提前谢谢。

0 个答案:

没有答案