Does the equals() method of an array do element-level checking?

John Zukowski

If you have two arrays in memory with the same elements, and ask first.equals(second), this does not do an element-by-element comparison. Instead, it behaves just like Object's equals() method, essentially asking if the variables point to the same place in memory:

    int a[] = {1, 2, 3};
    int b[] = {1, 2, 3};
    // This prints false
    System.out.println(a.equals(b));
To check for equality of two arrays, use Arrays.equals().
    // This prints true
    System.out.println(Arrays.equals(a,b));
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact