A String reference to the same object if you declare them without using the “new” keyword as follows:
String s1 = "Some string";
String s2 = "Some string";
System.out.println(s1 == s2);
However, contrary to what I expected from this with an array, it does not work:
char[] anArray = {'A', 'r', 'r', 'a', 'y'};
char[] oneArray = {'A', 'r', 'r', 'a', 'y'};
System.out.println("anArray == oneArray : " + (anArray == oneArray));
We didn’t explicitly mention that they are “new” arrays, so why don’t they refer to the same object on the heap?
source
share