Java Input Problems - how to compare strings [dupl

2019-08-03 03:51发布

问题:

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

回答1:

You should change

if (var1 == var2)
{
    return true;        
}
else
{
    return false;
}

to

if (var1.equals(var2))
{
    return true;        
}
else
{
    return false;
}

See this answer for the difference between the two



回答2:

Instead of if (same (var1, var2)) use if (v1.equals(v2)). No need to create a new method to compare two Strings. That's what equals() does.

== is used to compares references, not the contents of each String object.



回答3:

To 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()



回答4:

In Java the == is a reference equality operator.

It works with the following.

String var1 = "hello";
String var2 = "hello";
boolean cmp = var1 == var2;

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).



回答5:

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 use If(s==s1){System.out.print("Equal");}it would print equal.But if you check If(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 question

Equals method must be used.



回答6:

var1 == var2

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

var1.equals(var2)

If you want to compare their values and doesnt care about reference.