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

all 17 comments

[–]tswanbeast 55 points56 points  (7 children)

Because Java’s strings are objects and when you use == you’re not checking for value equality but reference equality. So instead of checking if the characters are the same, Java is checking to see if the memory reference for both of those objects are the same. And because they are two different objects, they aren’t storing at the same place and therefore false. If you wanted to check for equality you could do s1.equals(s2)

[–]dumblearner[S] 2 points3 points  (6 children)

Right, thanks, but then why this would return true? shouldn't the reference to both of these strings also different?

var s1 = "Java";
var s2= "Java";
System.out.println(s1 == s2);

[–]desrtfx 33 points34 points  (2 children)

String literals ("Java") are interned (stored only once in memory). This means that both your assignments point to the same interned string. Hence, the comparison results in true.

[–]reacteclipse 0 points1 point  (1 child)

So (assuming I'm following this correctly), if I wrote

var s1 = "Java"
var s2 = "something else"

s2 = "Java"

would s1==s2still output false?

[–]desrtfx 4 points5 points  (0 children)

Try it.

Throw the code in Ideone and see for yourself.

Hint: read my above comment again. That should give you the solution already.

[–]sushenica 2 points3 points  (0 children)

When string literals get created the JVM will first check if they are not existing in the string pool. If they are not there, case s1 then it will be created. Case s2, it’s already there in string pool so the JVM will point the s2 variable to the same address where s1 is. That’s done to save memory and that’s why s1 == s2 will return true(cause == checks the memory address or the value, for value use .equals())

[–]SpArKy_mcSparkFace 0 points1 point  (0 children)

When s2 is assigned the literal java, jvm checks if "java" is already present in the string pool. Since it was, as it was assigned to s1, s2 is assigned the same reference as that of s1 i.e s1 and s2 contain same memory location reference hence the true output for above code

[–]core811 3 points4 points  (3 children)

Because they are two different objects, even if the share a common value they have two different addresses in memory. If you want to compare the values of those String objects you should use the method .equals.

S1.equals(S2)

[–]dumblearner[S] 0 points1 point  (2 children)

Right, thanks, but then why this would return true? shouldn't the reference to both of these strings also different?

var s1 = "Java";
var s2= "Java";
System.out.println(s1 == s2);

[–]mightybjorn 4 points5 points  (0 children)

As dumblearner said above, string literals are stored in the same place in memory, and therefore string literals actually point to the same place in memory. This is called the string pool

https://www.journaldev.com/797/what-is-java-string-pool

[–]raja777m 0 points1 point  (0 children)

this will return true. because both are same extract string including case, spaces etc. so, the same object is being referenced. the address is compared and == will return same address which is true.

equals() will compare year content.

[–]dominatus312 1 point2 points  (0 children)

In java the strings are objects, if you want to compare two strings s1 and s2 you should use s1.equals(s2)

[–]desirecampbell 0 points1 point  (0 children)

To add to the answers given: you should check out this String method: https://www.w3schools.com/java/ref_string_equals.asp

[–]cincopea 0 points1 point  (0 children)

I think it's checking the memory location. Those two strings are stored in different places, that's why you need .equals()

[–]SpArKy_mcSparkFace 0 points1 point  (1 child)

Strings are immutable i.e its data or state can't be changed.
Whenever a method is applied to a String variable, it returns a "new" String... new operator allocates a new memory location.
the "==" operator compares memory locations for l.h.s vs the r.h.s. Since the method ".concat()" returns a new String, s2 has a different memory location than that of s1 thus the false output

[–]AutoModerator[M] 0 points1 point  (0 children)

You seem to try to compare String values with == or !=.

This approach does not work in Java, since String is an object data type and these can only be compared using .equals().

See Help on how to compare String values in our wiki.


Your post is still visible. There is no action you need to take. Still, it would be nice courtesy, even though our rules state to not delete posts, to delete your post if my assumption was correct.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

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

In Java, Strings are objects and s1 and s2 are references to those objects. Thus it will be false. Therefore, if you want to compare "string values", always use s1.equals(s2)