This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I compare strings in Java?
Java String.equals versus ==
I am newbie Java programming and I have a little question about compare string with
equals method and ==
Example 1: Doesn't work when get input from user but if it has set initial value it works fine.
if(str1 == str2)
System.out.println("equal");
Example 2: always works
if(str1.equals(str2))
System.out.println("equal");
if I have to compare string which command can be used.
String literals points to same location/value, thats why == on string literals works
When you get input from user, it will be treated as new String object.
equals() check for values equality whereas == check for reference equality.
Related
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 5 years ago.
I am encountering a weird problem. I was trying to compare two 128 bits strings and I believe every char of them match each other(and I tested comparing String.chartAt(#) several times) but when I do
if (String 1== String 2)
..
else
..
It went to else clause. Why is that?
When using == for comparison, you are checking if both variables refer to the same object (reference comparision). You should use strings equals() method in order to compare if they are equal in a sense they consist of same characters.
description of equals method in java documentation
public boolean equals(Object anObject)
Compares this string to the specified object. The result is true if
and only if the argument is not null and is a String object that
represents the same sequence of characters as this object.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
String s="rahul".substring(0, 1);
s==Character.toString('r') //2nd
2nd evaluates to false but it should evaluate to true since i am converting 'r' to a String.
Why i am getting false as a result?
By doing this i am able to make the condition true
//char s=name.charAt(0);
// s=='r'
;
Try it as:
s.equals(Character.toString('r'). For more details What's the difference between ".equals" and "=="?
Instead of comparing Strings with == you should use equals().
String s="rahul".substring(0, 1);
System.out.println(s.equals(Character.toString('r')));
Use .equals instead, see code below:
//current code
System.out.println(s==Character.toString('r'));
//change to be as follows
s.equals( Character.toString('r'));
System.out.println(s.equals( Character.toString('r')));
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
== tests for reference equality.
.equals() tests for value equality.
I saw this post How do I compare strings in Java? . And I still don't get why you get false when you compare
// ... but they are not the same object
new String("test") == "test" // --> false
or
String str1 = new String("JAVA");
String str2 = new String("JAVA");
System.out.println(str1==str2);
Does this happen because they have different name or what is the reason ?
That happens because the == operator compares memory addresses, not contents.
Note that String is an object, not a primitive, that is probably why people get confused, you can compare primitives with == and it will be fine, but with objects you want to compare their contents.
Use equals.
str1.equals(str2);
This question already has answers here:
Comparing strings with == which are declared final in Java
(6 answers)
Closed 9 years ago.
while comparing to strings we can do using == or .equals()
In == we know that it checks for references but in .equals() it checks for contents.
So suppose if there are 2 strings say
String s="SO";
String s1="SO";
so in this case s1==s and s.equals(s1) both will give true.
But here it gives me false
So what I assume is + is high priority than ==
so in this case
System.out.println(""+s1==s);
it will be splitted like (""+s1)==s and now ""+s1 will be a new String and hence the new String will never be equal to s so its printing false
I am just interested to know whether I thought is right or not
""+s1 creates a new String Object on the heap (since it is not declared as final). So, the references will not be same.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Difference in string comparison result b/w == and String#replace with == [duplicate]
(6 answers)
Closed 9 years ago.
System.out.print("a".replace('a','1')=="a".replace('a','1'));
true was expected as replace() returns String and String comparison is possible with ==.
But the above code prints false. Explanations?
== checks if they are the same object in memory, so at the same location.
Since these are two distinct strings that just happen to have the same content you need to compare with .equals which compares values not reference.
No, string comparison is not advisable with ==. In certain cases e.g.:
new String("test") == new String("test")
it returns false.
All this is because theoretically Java should search for certain string in string pool, but in practice there are operations that returns new object instead of the one that resides in string pool.
This is very important to always use "equals" method instead of ==!!