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 ==
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:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I'm getting the user input like so:
Scanner keyboard = new Scanner(System.in);
String input = keyboard.nextLine();
When I compare it like this
if(input == "abc"){
System.out.println("Match!");
}else{
System.out.println(input + "\nabc");
}
If I type "abc" I see
abc
abc
Instead of the expected match. What's going on?
You are comparing String references, not String values. Use the String.equals method instead.
== is comparing memory locations. you need to use input.equals("abc") instead.
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.
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:
How do I compare strings in Java?
(23 answers)
String.equals versus == [duplicate]
(20 answers)
Closed 9 years ago.
Anybody ever seen the following statement running false ?
String BloodyHell = "Unbelievable";
if (BloodyHell == BloodyHell) >> false
Although instead of bloody hell, I've got: |-,-| or |o,-| or |-,o| or |o,o| or XXXX.
None of these comes TRUE though. While debugging the exact thing is still false.
Somebody please help me out here. Here's the snippet:
public String doStats()
{
String[] pattern = splitPattern();
for (int i = 0; i < pattern.length; i++)
{
if (pattern[i] == "|-,-|")
frontClosed++;
if (pattern[i] == "|o,-|")
left++;
if (pattern[i] == "|-,o|")
right++;
if (pattern[i] == "|o,o|")
frontOpened++;
if (pattern[i] == "XXXX")
noFace++;
}
}
In Java you should use the String#equals method to compare string values instead of ==. The == operator compares object references to see if they are the same object, which is why you keep getting false. The object references are different, even if the contents of the strings are equivalent.
if (pattern[i].equals("|-,-|"))
There is a difference between == and equals().
When comparing strings, you want to use equals(); == is used to compare if they are the same thing in memory.