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.
Related
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!
This question already has answers here:
Comparing strings with == which are declared final in Java
(6 answers)
What is the difference between these two ways of initializing a String?
(3 answers)
Closed 7 years ago.
I am unable to recognize the difference in the following declarations of Strings in Java.
Suppose I am having two string
String str1="one";
String str2="two";
What is the difference between
String str3=new String(str1+str2);
and
String str3=str1+str2;
In both the above declarations, the content of str3 will be onetwo.
Suppose I create a new string
String str4="onetwo";
Then in none of the above declarations,
if(str4==str3) {
System.out.println("This is not executed");
}
Why are str3 and str4 not referring to the same object?
str1 + str2 for non-compilation-constant strings will be compiled into
new StringBuilder(str1).append(str2).toString(). This result will not be put, or taken from string pool (where interned strings go).
It is different story in case of "foo"+"bar" where compiler knows which values he works with, so he can concatenate this string once to avoid it at runtime. Such string literal will also be interned.
So String str3 = str1+str2; is same as
String str3 = new StringBuilder(str1).append(str2).toString();
and String str3 = new String(str1+str2); is same as
String str3 = new String(new StringBuilder(str1).append(str2).toString());
Again, strings produced as result of method (like substring, replace, toString) are not interned.
This means you are comparing two different instances (which store same characters) and that is why == returns false.
Java does not have memory of "how this variable got the value", therefore it really does not matter which method you use, if the result is same.
About comparing, if you compare strings with ==, you are comparing address of objects in memory, because String is not primitive data type, not values. You have to use if(str4.equals(str3))
Because Strings in Java are immutable the compiler will optimize and reuse String literals. Thus
String s1 = "one";
String s2 = "one";
s1 == s2; //true because the compiler will reuse the same String (with the same memory address) for the same string literal
s1 == "o" + "ne"; //true because "Strings computed by constant expressions are computed at compile time and then treated as if they were literals"
s3 = "o";
s1 == s3 + "ne"; //false because the second string is created a run time and is therefore newly created
for a reference see http://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.5
Strings are kind of tricky, because there is some effort to share their representation. Plus they're immutable.
The short answer is: unless you're really working in a low level, you should never compare strings using "==".
Even if it works for you, it will be a nightmare for your teammates to maintain.
For a longer answer and a bit of amusement, try the following:
String s1= "a" + "b";
String s2= "a" + "b";
String s3=new String("a"+"b");
System.out.println(s1==s2);
System.out.println(s3==s2);
You'll notice that s1==s2 due to the compiler's effort to share.
However s2 != s3 because you've explicitly asked for a new string.
You're not likely to do anything very smart with it, because it's immutable.
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)
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:
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"}