0
    String test1 = "test";
    String test2 = "test";

    System.out.println(test1 == test2); // true

test1 and test2 points to the same object, so the outcome is true.

    String test1 = new String("test");
    String test2 = new String("test");

    System.out.println(test1 == test2); // false

test1 and test2 points to the different object, so the outcome is false.

so the question is that, what is the difference between,

    int[] test = {1,2,3}; // literal
    int[] test = new int[] {1,2,3}; // non-literal

I am confused of this since,

    int[] test1 = new int[]{1,2,3};
    int[] test2 = new int[]{1,2,3};

    System.out.println(test1 == test2); // false

and

    int[] test1 = {1,2,3};
    int[] test2 = {1,2,3};

    System.out.println(test1 == test2); // also prints false

I expected that the latter case`s outcome would be true, the same reason with the case of String example above.

Is test1 and test2 pointing at the different array object?

jwkoo
  • 2,393
  • 5
  • 22
  • 35
  • 2
    String and int are not the same. https://stackoverflow.com/questions/2486191/what-is-the-java-string-pool-and-how-is-s-different-from-new-strings – Murat Karagöz Mar 16 '18 at 10:27
  • 1
    `int[] test1 = {1,2,3};` creates a new array *every time*. – assylias Mar 16 '18 at 10:27
  • @Murat K. I already read that article. I know String and int are not the same. but its int array. so test1 points to that array in the heap area. am I wrong? – jwkoo Mar 16 '18 at 10:28
  • @assylias creating string object with String class in literal way overrides the previous one, but not the array object created with literal way? – jwkoo Mar 16 '18 at 10:30
  • 1
    Are the for-loops to confuse the readers? Do you believe the result may change over time, the JVM might realize, that you're sceptic about the outcome, if you as the same question 3 times? – user unknown Mar 16 '18 at 10:42
  • I`ll change it. – jwkoo Mar 16 '18 at 10:47

2 Answers2

2

Java int[] are not ever intern'd. Only String (and the wrapper types for limited values). tl;dr Don't compare object equality with ==. That only compares references with instance types. Here array equality can be determined with Arrays.equals(int[], int[]) and String with String.equals. Arrays don't override Object#equals(Object).

int[] test1 = { 1, 2, 3 };
int[] test2 = { 1, 2, 3 };
System.out.println(Arrays.equals(test1, test2)); // <-- true
System.out.println(test1.equals(test2)); // <-- false

As for why they differ - Java String is immutable (as are the primitive types). Here we can change a value in one of the arrays. We would be surprised (as users) if the other also changed.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Thanks, so int[] arr = {1,2,3}; and int[] arr = new int[]{1,2,3}; internally has no difference right? (opposite to the string) – jwkoo Mar 16 '18 at 10:36
  • @jwkoo `new String(String)` is a pointless copy constructor. Your two `int[]` examples are identical (the first is syntactic sugar for the second). – Elliott Frisch Mar 16 '18 at 10:38
  • I am sorry but what does it mean "pointless"? since the case of String test = new String("o");, that test String variable points to the string "o" – jwkoo Mar 16 '18 at 10:41
  • @jwkoo `String test = "o";` - why are we copying `String` literals? What is the point to using more memory to copy an immutable object? – Elliott Frisch Mar 16 '18 at 10:42
  • I cannot understand your meaning of 'copying' , https://stackoverflow.com/questions/3052442/what-is-the-difference-between-text-and-new-stringtext says that literal created string object goes inside to the heap-constant pool, but that non-literal string object goes in to the heap. its totally different. I think that not just copying, but to create new object. – jwkoo Mar 16 '18 at 10:46
  • @jwkoo **copying** `==` **new object**. Now that we are on the same page; **why** new object? It is pointless. – Elliott Frisch Mar 16 '18 at 10:50
  • Thanks for your comment, actually I need to search more about what you have mentioned. The reason why I used new String is just to show the different outcome compared to arr int. – jwkoo Mar 16 '18 at 11:05
0

for declaration array are both ways possible

int[] a = new int[] {1,2,3,4,5};
int[] b = {7,8,9,10};

but after the declaration, with the first way you can assign new array to existing variable of the same type, with second way no.

a = new int[] {1,1,1,1};  // ok
b = {2,2,2,2,2};          // error: illegal start of expression
zemiak
  • 361
  • 2
  • 5