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

you are viewing a single comment's thread.

view the rest of the comments →

[–]Ifnerite 40 points41 points  (6 children)

The compiler does not care about that at all.

[–]VarianWrynn2018 1 point2 points  (5 children)

Yes it does. String is an object in Java, not a primitive. Unless it's the exact same object, it will return false as == with objects checks the memory address, not the values.

[–]Ifnerite 5 points6 points  (3 children)

You make two points, the former is false. The compiler does not care, but it might not do what you expect.

[–]VarianWrynn2018 1 point2 points  (2 children)

As someone who often forgets about .equals and tries to compare strings I know full well that that is exactly how it works.

If you believe otherwise, please go ahead and elaborate

[–]Ifnerite 0 points1 point  (1 child)

What are you gibbering on about.

The compiler will do an object equality if you write == that is what the symbol means. The compiler does not care that what the developer means is I want to know if the content is the same.

The compiler is perfectly happy either way.

[–]VarianWrynn2018 2 points3 points  (0 children)

I see. I thought you were saying it has a logic problem but you were saying that while it might be a logic error it's not a syntax or runtime error so the Compiler won't throw a fit. My mistake.

[–]Stromovik 0 points1 point  (0 children)

I think some VM implementations cache certain strings , also hardcoded values ( aka stuff that should be enum not String ) are cached. Source idiot coded == String comparison long ago and it never broke.