1

I am confused with a comparison between aliases in Strings and aliases in Arrays.

String a = "hello";
String b = "hello";
a == b;
>>> true

int [] a = {1,2,3};
int [] b = {1,2,3};
a == b
>>> false

I knew in Strings, when you call new String method, it would direct to a different address. Otherwise, it would take the previous address with the same String literal. However, things do not work for arrays. Can someone explain why it gives false?

  • Strings are immutable which means that they can be interned (or cached) and resused. So in your string example, both are the same object and both would be true when using ==. However, arrays are not immutable and thus you are showing two separate objects which would equate to false. And normally you would not compare strings with == but use .equals(). – WJS Jun 12 '19 at 20:22
  • @Raedwald - Not sure this is a duplicate of the one you pointed out. This question isn't how should one compare Strings, it is why do Strings behave differently than arrays. – dan.m was user2321368 Jun 13 '19 at 15:09

4 Answers4

0

== compares memory locations. In your second example, they are different memory locations.

For the first example, Java does some optimization when the String are created in order to make sure all "equal" String literals point to the same memory location; therefore, the comparison returns true

Tiberiu
  • 990
  • 2
  • 18
  • 36
  • So java will create an array at different memory location even though new array method is not use, right? –  Jun 12 '19 at 20:23
  • 1
    @StellaZhu Yes, it has to. Otherwise, changing `a[0]` would also change `b[0]`. Since `String`s are immutable and can't be changed, Java is allowed to use the same memory address for both as an optimization. – that other guy Jun 12 '19 at 20:25
0

Watch out ! == is a pointer comparison. It is true if you are comparing the exact same object (memory location).

In your particular case, String are immutable, and the compiler only creates "hello" once. That's an optimization.

For example, if you change your snippet like this the == returns false :

String a = "hello";
String b = "hell";
b += "o";
System.out.println(a == b);

In general when you compare string, you use .equals(). So with the previous example :

a.equals(b)

does return true.

HTH !

Highbrainer
  • 750
  • 4
  • 15
0

To add to what Tiberiu has said it may be clearer to show that {1,2,3} is shorthand for new int[]{1, 2, 3} which is making a new memory location. If you use:

    String a = "hello";
    String b = new String("hello");

It will return false because they are pointing to different memory locations (you should really never do this with String.

In addition if you do:

    int [] a = {1,2,3};
    int [] b = a;

Clearly it will now return true since b is now pointing to same location as a.

EDIT:

To try and clarify it a little more, imagine the array section looked like this:

    int [] a = new int[]{1,2,3};
    int [] b = new int[]{1,2,3};

This is the same code as what you wrote but not with shorthand, but it should be clearer now you are declaring two new arrays in different locations, so == will be false.

Nexevis
  • 4,647
  • 3
  • 13
  • 22
0

As Strings are immutable, Java can perform an optimization: when it detects that the two Strings are both initialized to refer to the same value: it only needs to make a single object of that value, and can have both String variables refer to it.

As arrays are mutable, if it were attempt to make the same optimization, a change to a (e.g. a[1]=7) would cause b to change too. This behavior isn't what one would expect, so it is not done. If you explicitly wanted that behavior, you would have explicitly set b to refer to what a referenced (e.g. int[] b = a).

  • So in java reference, is String the only one that has that kind of optimization? Since it is the only one immutable? –  Jun 13 '19 at 05:19
  • Also, I want to make sure about the concept of reference. When you refer array b to a, is that the same as making b as an alias of a? In that way, what you change in a will also change b in the same way because arrays are mutable. However, when you make String b as an alias of a. Things are different because Strings are immutable, so java only takes copies from Strings and makes changes. –  Jun 13 '19 at 05:27
  • @StellaZhu - In Java, there are two distinct concepts: variables and objects. There are two styles of variables, those that "hold" their value directly, and those that "hold" a reference to an object. The style is a function of the type of the variable: an "int" variable "holds" the vale directly, where as a "String" or an "int[]" variable hold references. When you set "int[]b = a", you are telling Java to make b refer to the thing that a is currently referring to - you are not making a copy. When you do "String d = a", you are doing exactly the same thing. – dan.m was user2321368 Jun 13 '19 at 15:13