0

Possible Duplicate:
Difference Between Equals and ==
String comparison and String interning in Java

Pretty self-explanatory question this time.

EDIT: I understand == is not equals. What I did not understand is why both a in "a" == "a" were assigned or treated as if they were same object instance (they are).

EDIT DID SOMEBODY READ THE QUESTION OR JUST LIKE PRESSING CLOSE BUTTON? IT HAS NOTHING TO DO WITH ANY OF THESE ABOVE. TELL ME WHERE DO I MENTION EQUALS METHOD.....

Community
  • 1
  • 1
Whimusical
  • 6,401
  • 11
  • 62
  • 105
  • 1
    `String('a')` will produce two different string objects, whose internal identifiers are different, hence can't be compared as equal. – Marc B Aug 01 '12 at 17:44
  • I absolutly know the difference between equals and ==, so it has nothing to do with your link. What I did not know is that string literals were treated as constants. Look my question never asked for operators, it was "why same syntax has different behaviors". I edited later, so I concrete to the real thing: the second part or why "a"=="a" is said to be true – Whimusical Aug 01 '12 at 18:02
  • @user1352530 If you properly understand the implications of the linked question, then you wouldn't have asked this question. – NominSim Aug 01 '12 at 18:04
  • Yes because I though that "a" was an alias for new String("a"), I would not ever said that the compiler is smart enough to look if this string has been used and point to the object already allocated in memory that matches content. It is a syntax issue. I guess then than 3 == 3 could also be true and any primmitive type with same lgical content reallocated? I guess also than String a="a" and String b="a" will point both to same physical object – Whimusical Aug 01 '12 at 18:06
  • And it is not a normal expected behavior from the theorics of pointers. Its more of a compiler microptimization. Just try to do the same with most of other objects through anonymous classes for example. – Whimusical Aug 01 '12 at 18:15
  • I really think is not a dup neither. The question is about equals and ==. My question is a doubt about commpilator interpreting two instances of string as same object when they are declared with the direct syntax "a" – Whimusical Aug 01 '12 at 20:58
  • I dont even mention the word equals in the question and i have two different links of equals vs == which leads to 4 closing votes. I dont mind the points or nothing is just I cannot understand people and need of distroying others questions. I also cannot understand why a negative vote if the question is clear and real – Whimusical Aug 01 '12 at 21:01

7 Answers7

14

"a" == "a" gives true because "a" will be treated as String literal and pooled. So, both "a" points to same instance, because both references points to same object, == returns true.

When you say new String("a"). Brand new object will be created on Heap and different references, so == returns false, you need to use .equals().

kosa
  • 65,990
  • 13
  • 130
  • 167
  • 2
    For what it's worth, that constructor parameter would be equal to the other literals too- it's the construction of the new object that causes the differentiation to occur. – Platinum Azure Aug 01 '12 at 18:05
  • 3
    No one has yet actually stated this, and it's relevant: the String literal value found in this pool is also referred to as the 'internal value', or 'intern value', which can be obtained via the String.intern method; `(new String("a").intern()) == "a";` It many cases using the intern value allows for higher performance by allowing you to use `==` instead of `equals`, however you absolutely **must** ensure all values are intern'ed. – Richard Sitze Aug 01 '12 at 18:06
  • @PlatinumAzure: Both are valid points. Thanks for adding as comment. – kosa Aug 01 '12 at 18:08
2
  • == checks for equality in references (pointers of memory)
  • "a" is a string literal and it's stored in constant data table (so that every use of it will point to the same literal)
  • new String("a") allocates a string object with the content of "a" but it is a stand alone object with is own memory space.
  • every new always allocates a different obect, so two allocated object can't have same reference
Jack
  • 131,802
  • 30
  • 241
  • 343
1

You have to use "a".equals("a");

Matt Westlake
  • 3,499
  • 7
  • 39
  • 80
1

Remember that ==checks if the objects are the same. Two objects can be different but equal to each other.

new String(“a”) == new String(“a”): Two different String objects are created with the same value, so == returns false because the objects are different.

“a”==“a”: The strings are interned to the same Object, so == returns true.

If you want to check if two objects have the same value, then try using .equals()

Prashant Kumar
  • 20,069
  • 14
  • 47
  • 63
1

When you use new String("a") it creates a new memory location. When you're comparing objects (String is an Object) with == it compares the memory locations and thus you get false. The new forced it to create 2 different memory locations. The "a" == "a" compares the memory locations for each and since they are string literal's they have same memory reference. The proper way to compare string is to use the .equals() method from the String class.

minhaz1
  • 733
  • 1
  • 5
  • 13
1

"a" == "a" - Here process keept this string in a text segment, which is read only data. And you are just comparing the address of the text segment where this string is present. Which will give you true.

But new String("a") will allocate memory dynamically in heap and it will copy the string "a" to that dynamically allocated place. So new String("a") = new String("a") will allocate memory two times in heap. And you are comparing those two address its absolutely different. Because it has allocated memory in heap two times.

rashok
  • 12,790
  • 16
  • 88
  • 100
-1

Java creates a string-pool for strings that are know in design time. If you write something like

string a = "a"; string b = "b"; a == b will return true because java creates only one object with the same address.

If you write something like

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

this will return false because two objects are created during runtime with different addresses.

The == operator compares the addresses of the objects, not the values.

user1567896
  • 2,398
  • 2
  • 26
  • 43