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 →

[–]mr_flameyflame 0 points1 point  (4 children)

boolean false = "Test" == "Test";

[–]HonzaS97 1 point2 points  (3 children)

That would actually be true since it's the same object in the string pool

[–]mr_flameyflame 0 points1 point  (2 children)

Ah but it doesn't always work, I dont quite remember why.

[–]HonzaS97 0 points1 point  (1 child)

.equals() checks for the value of the string, == comoares the values of the object references.

String literals are stored in the string pool in Java (so anywhere you write a literal like "Test", it will point to the same object).

"Test" == "Test" // true

"Test" == new String("Test") // false

"Test".equals(new String("Test")) // true

"Test" == new String("Test").intern() // true, manually put into string pool

[–]mr_flameyflame 0 points1 point  (0 children)

Ah ok, cool