This question already has an answer here:
- How do I compare strings in Java? 23 answers
This seems to be pretty simple, but I have been stucked here for a couple of hours.
I have a doubt when you have to compare two Strings in Java.
if I just do something like this:
String var1 = "hello";
String var2 = "hello";
and then compare these two words in another function, the result will clearly be true.
But the problem is when I have to compare two words that come from an input. Here is my code:
import java.util.Scanner;
public class Compare{
public static void main(String[] args){
Scanner Scanner = new Scanner (System.in);
System.out.println("Enter first word: ");
String var1 = Scanner.nextLine();
System.out.println("Enter second word: ");
String var2 = Scanner.nextLine();
if (same (var1, var2))
System.out.println("Yes");
else
System.out.println("No");
}
public static boolean same (String var1, String var2){
if (var1 == var2)
return true;
else
return false;
}
}
I have tried several times (clearly entering the same word) and the result is always False.
I don't know why this happens. What am I missing?
This is my first time in Java. I will appreciate any kind of help. Thanks
You should change
to
See this answer for the difference between the two
Instead of
if (same (var1, var2))
useif (v1.equals(v2))
. No need to create a new method to compare twoString
s. That's whatequals()
does.==
is used to compares references, not the contents of eachString
object.In Java the
==
is a reference equality operator.It works with the following.
just because they are string literals and they are allocated in the same place inside the string table, so both variables point to the same string.
If you are fetching data from another source the strings are dynamically allocated, hence you should use the
var1.equals(var2)
(and you should ALWAYS use that one when comparing two objects).The equality operator(==) checks the refernce of string first then checks value of string. While equals method checks the value first. So,in this case equals method should be used instead of equality operator.
String s="hello"; String s1="hello"; String s3=new String("hello")
In the above code snippet if you useIf(s==s1){System.out.print("Equal");}
it would print equal.But if you checkIf(s==s3){System.out.print("unqual");}
it wouldn't print unequal. so,you can see that even strings s and s3 are equal,output is wrong.Therefore,in this scenario like program in questionTo be more accurate, with Strings in Java sometimes you can use == instead of .equals, if your string has been interned. Remember that == always compares the object references, not the contents of the object. Interning a String means that you will get the same object reference back and this is why == works with interned Strings.
Please read the Javadoc here to understand this more clearly:
String.intern()
sometimes works because VM allocates the same memory both the variables for memory optimization and thus having same reference. That cannot be always the case so it's better to use
If you want to compare their values and doesnt care about reference.