This question already has answers here:
What is the Java string pool and how is "s" different from new String("s")? [duplicate]
(5 answers)
How do I compare strings in Java?
(23 answers)
Closed 5 years ago.
I'm preparing for the OCA and I am puzzled by reference equality in strings.
So given Case 1:
String s = "Hello";
if("Hello" == s){
System.out.println("TRUE");//prints TRUE
}else {
System.out.println("FALSE");
}
But in Case 2:
String s = "";
s += "Hello";
if("Hello" == s){
System.out.println("TRUE");
}else{
System.out.println("FALSE"); //prints FALSE
}
I know that strings are immutable, but "==" checks for reference equality, and in the second case "s" is a reference that initially points to an empty string and then points to "Hello". So, why does the second case return false?
What am I missing?
Regarding:
What is the Java string pool and how is "s" different from new String("s")?
In my question, in both cases "s" is a string literal and found in the string pool. The above mentioned questions is about a string literal and a String object (which gets memory allocated).
Regarding:
How do I compare strings in Java?
My question asks "why", not "how".
#Mark Rotteveel
Yes I understand that ""+"Hello" is a different string than "", but we are discussing reference equality where "s" is a reference which first points to a string "", and then the same reference "s" points to another string "Hello". In both cases, before the if-clause, the reference "s" points to "Hello", but the results to the if-clause are different.
Thanks for having a look!
Related
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
String s1 = "abcdef";
String s3 = new String(s1);
if(s3 == s1){
System.out.printf("yes");
}else{
System.out.printf("no");
}
Why is not print yes? Who can explain to me?
Change s3 == s1 to s3.equals(s1).
The == operator or will check if they're both the same object, rather than what their string value is.
When you are dealing with Objects, you should use their equals method, not the ==
Check this for further explanation:
Java String.equals versus ==
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Java comparison with == of two strings is false? [duplicate]
(12 answers)
Closed 9 years ago.
Why is it that comparison of two strings having identical contents (s1==s2) returns true but (s3==s4) returns false if we initiate s1 and s2 with "=" but s3 and s4 with String constructor? #JAVA
Like
String s1="s";
String s2="s";
String s3= new String("s");
String s4= new String("s");
String s1 is a REFERENCE not an OBJECT. This means when you compare s1 == s2 you are comparing the references, not the contents of what those references point.
When you have two String literals which are the same, they are cached and the same object is used. This is done to save space. This means two string literals which have the same contents point to the same object.
When you create two new objects, they have different references so they are not equal, even if s3.equals(s4) is true.
I suggest you look at the String.intern() method which details how Strings are pooled.
So these are all true.
s1 == s2;
s1 == s3.intern();
s3.intern() == s4.intern();
s1 == s1.intern();
s1 == s1.intern().intern().intern();
s3 != s3.intern();
In hindsight, I think Java should have had a === for comparing references and == for comparing contents i.e. calling equals as this is a common source of confusion for developers who don't understand the difference between references and objects in Java.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
String firstanswer = scan.nextLine();
if(firstanswer == answer2)
{
System.out.println("OK lets get started");
}
else
{
System.out.println("That is incorrect, of course you want to play");
}
//answer2 is set to "yes", i declared it above
make it firstanswer.equals(answer2) instead of firstanswer == answer2.
When you want to check for equality of String in java then use equals method instead of == operator.
equals method checks whether the contents of String objects are same
== operator checks whether both the reference variables refer to same String object
To understand about strings and equality, read String comparison with equals and assignment operator It will help you understand the concept well.
Use equals() instead of == to compare strings.
if(firstanswer.equals(answer2)) is what you're looking for.
firstanswer and answer2 are pointers to string objects. == checks to see whether the pointers are equal (whether they point to the same object), while equals() compares the contents of the two strings and returns a boolean representing whether or not the contents are equal.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I have an if statement that takes a string, and if another string has the same value as that string do 1 thing, and if the variable doesnt equal that string do another thring
here is my code
if(Pos != "D"){
System.out.println("doesnt = D");
}
if (Pos == "D" ){//WHY ISNT THIS WORKING
System.out.println("it does = D");
}
It recognizes when the variable doesnt = D and prints "doesnt = d" but when the variable = D it does nothing. I dont know why.
thanks
Never compare Strings with == or != since these check to see if two String variables refer to the same object reference, and this is not what you're interested in. Instead use the equals(...) or equalsIgnoreCase(...) method to see if the two Strings have the same chars in the same order as that's what really matters here. i.e.,
Use equals to compare strings :
if ("D".equals(Pos))
This question already has answers here:
String comparison and String interning in Java
(3 answers)
How do I compare strings in Java?
(23 answers)
String.equals versus == [duplicate]
(20 answers)
Closed 10 years ago.
I'm new to this site and didn't realize there were other questions that answered what I'm asking. I've figured it out and I will delete this post as soon as it lets me. Thank you.
I just started learning java again and I have a quick question.
Usually, using == to compare strings would not work and you would have to use .equals instead.
But now while coding, I found they are doing the same thing when they are not supposed too and I'm trying to figure out why.
Here's the code.
String s = "Hello";
String x = "Hello";
if (x == s){
System.out.println("It is working!");
}//end if
else {
System.out.println("It is not working");
}//end else
if (x.equals(s)){
System.out.println("Match");
}//end if
else {
System.out.println("No match");
}//end else
Basically you're seeing the result of string interning. From section 15.28 of the JLS:
Compile-time constant expressions of type String are always "interned" so as to share unique instances, using the method String.intern.
So your two variable values actually refer to the same strings. If you use:
String s = new String("Hello");
String x = new String("Hello");
... then you'll see the behaviour you expect.
Reason is that your vars x and s have the same reference hence == behaves same as .equals.
When Java compiler optimizes your string values (literals), it determines that both x and s have same value and hence you need only one String object. As result, both x and s point to the same String object and some little memory is saved. It's safe operation since String is an immutable object in Java.
well, In this particular case there is only one string object created and cached in the string constant pool and both of the reference variables refer to the same string object which is stored in the string constant pool . thus you == test passes
String s = "Hello"; Line 1
String x = "Hello"; Line 2
When line 1 is executed one string object (Hello) is created and cached in String Constant pool .
**String Constant Pool:** {s--->"Hello"}
when line 2 is executed, it first checks if there is any object with the same value in string constant pool, if there exists one. it simply points the reference to the already created object in the pool.
**String Constant Pool:** {s,x--->"Hello"}