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.
Related
This question already has answers here:
Why should a Java class implement comparable?
(10 answers)
Closed 6 years ago.
String ntext;
ntext = something;
String currentLine;
currentLine = something;
while(ntext.compareTo(currentLine) != 0){
//some condition
}
Here i want to know what that compareto actually do.
One more questin what we can use to compare two objects?
If those variables are strings (i assume so) it checks if they are equal, returns 0 if so, and another number if not. See the JavaDoc here: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#compareTo%28java.lang.String%29
More specifically, it goes through both strings character by character. When it finds a string of one that is not equal to the other, it returns a number representing whether the differing character is more than, less than, or equal to the corresponding character in the other string.
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)
Closed 9 years ago.
System.out.println(characters.get(selected).getName());
// The name printed is "Mario" But the if statement will not
// work as though the two strings are diffrent but they are the same.
if(characters.get(selected).getName() == "Mario"){
playSound("sounds/clickmario.wav");
}
Comparing two strings and when I debug the comparison is "Mario" to "Mario" so the if statement should be true but its false because nothing inside the if statement is being read. Why is this happening? I have tried assigning this .getname to a tempString and comparing it but still when they are the same string the statement results as false. Please help
You have to use .equals() for string comparison in java
if(characters.get(selected).getName().equals("Mario")){
playSound("sounds/clickmario.wav");
}
Refer this for String comparison.
and
for basics of String refer this.
use
if (stra === strb)
it should work in javascript, for java
if (stra.equals(strb))
Then it should work too
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 ==!!
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.