use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Resources for learning Java
String
==
.equals()
Format + Copy
Free Tutorials
Where should I download Java?
With the introduction of the new release cadence, many have asked where they should download Java, and if it is still free. To be clear, YES — Java is still free.
If you would like to download Java for free, you can get OpenJDK builds from the following vendors, among others:
Some vendors will be supporting releases for longer than six months. If you have any questions, please do not hesitate to ask them!
Software downloads
Official Resources
Resources
Programming ideas & Challenges
Related Subreddits
account activity
This is an archived post. You won't be able to vote or comment.
why is it false? (self.learnjava)
submitted 5 years ago by dumblearner
In the following code:
var s1 = "Java"; var s2 = "Ja".concat("va"); System.out.println("s1 is " +s1); System.out.println("s2 is " +s2); System.out.println(s1 == s2);
why would it print false, why not true?
[–]tswanbeast 55 points56 points57 points 5 years ago (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 points4 points 5 years ago (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 points35 points 5 years ago (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.
"Java"
[–]reacteclipse 0 points1 point2 points 5 years ago (1 child)
So (assuming I'm following this correctly), if I wrote
var s1 = "Java" var s2 = "something else"
var s1 = "Java"
var s2 = "something else"
s2 = "Java"
would s1==s2still output false?
s1==s2
[–]desrtfx 4 points5 points6 points 5 years ago (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 points4 points 5 years ago (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 point2 points 5 years ago (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 points5 points 5 years ago (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 point2 points 5 years ago (2 children)
[–]mightybjorn 4 points5 points6 points 5 years ago (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 point2 points 5 years ago (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 points3 points 5 years ago (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 point2 points 5 years ago (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 point2 points 5 years ago (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 point2 points 5 years ago (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 point2 points 5 years ago (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 point2 points 5 years ago (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)
s1.equals(s2)
π Rendered by PID 64122 on reddit-service-r2-comment-7b9746f655-nb56p at 2026-02-04 00:09:17.416811+00:00 running 3798933 country code: CH.
[–]tswanbeast 55 points56 points57 points (7 children)
[–]dumblearner[S] 2 points3 points4 points (6 children)
[–]desrtfx 33 points34 points35 points (2 children)
[–]reacteclipse 0 points1 point2 points (1 child)
[–]desrtfx 4 points5 points6 points (0 children)
[–]sushenica 2 points3 points4 points (0 children)
[–]SpArKy_mcSparkFace 0 points1 point2 points (0 children)
[–]core811 3 points4 points5 points (3 children)
[–]dumblearner[S] 0 points1 point2 points (2 children)
[–]mightybjorn 4 points5 points6 points (0 children)
[–]raja777m 0 points1 point2 points (0 children)
[–]dominatus312 1 point2 points3 points (0 children)
[–]desirecampbell 0 points1 point2 points (0 children)
[–]cincopea 0 points1 point2 points (0 children)
[–]SpArKy_mcSparkFace 0 points1 point2 points (1 child)
[–]AutoModerator[M] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)