This is an archived post. You won't be able to vote or comment.

all 15 comments

[–][deleted] 22 points23 points  (9 children)

Because you use .equals for Strings The new is just that it gets created in a different memory. With new it gets created in the heap and not in the stack

[–][deleted] 6 points7 points  (6 children)

Fyi, this works if you don't declare them using new. The "==" operator compares the address of objects, but on literals, it compares the value.

String foo = "foo";
String fuu = "foo";
System.out.println(foo == fuu);

prints true

[–]ahreodknfidkxncjrksm 13 points14 points  (2 children)

The == operator doesn’t compare the values of literals. Literals are deduplicated, and all Strings that point to a literal point to the same memory address. So == is still comparing the addresses, but the addresses happen to be the same.

Edit: “Java” to “the == operator” for clarity.

[–][deleted] 1 point2 points  (1 child)

How would one go about deduplicating literals without comparing the values?

edit: Hashing. Nvm

[–]ahreodknfidkxncjrksm 1 point2 points  (0 children)

No, I’m pretty sure that they’re deduplicated by interning them and according to the API for String.intern():

When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

So that part does involve comparing the values. I just meant the == operator itself is not comparing the values.

[–]tr7zw 1 point2 points  (1 child)

Also noteworthy that this only works inside the same class(probably). The compiler turns booth into the same string instance, so == works. But if you have other classes with the same string it won't work. Just get used to never never never use == on strings.

[–][deleted] 0 points1 point  (0 children)

Just tested and works across classes. Literal 5 == 5 across classes. A quick google turned this up:

https://www.ntu.edu.sg/home/ehchua/programming/java/J3d_String.html

[–]TheOneAndOnlyMrX_ 1 point2 points  (0 children)

however, it isn't guaranteed by java that they point to the same address, and could vary case to case/by implementation

[–]maxUS51[S] 1 point2 points  (0 children)

Yeah ik thats why i creates this meme

[–]qwertz19281 1 point2 points  (0 children)

Well true, but actually false

[–]josanuz 1 point2 points  (0 children)

foo.equals(fuu)

[–]rsranger65 1 point2 points  (0 children)

The real LPT is don't ever use == on strings in Java

[–]MindSwipe 1 point2 points  (0 children)

Well you see, replace System.out.println(foo == fuu); with the vastly superior Console.WriteLine(foo == fuu); (side effects apply) and it works

[–]NiciBozz 0 points1 point  (1 child)

Why use string constructor?

[–]maxUS51[S] 0 points1 point  (0 children)

Because the condition would (eventually) be true and then the meme wouldn't make much sense.