The class test1 {
Public static void main (String [] args) {
Scanner sc=new Scanner(System.in);
System. The out. Print (" user name: ");
String name=sc.next();
//char name1=name. CharAt (0);
System. The out. Print (" password: ");
Int the password=sc. NextInt ();
Strings str1="green";//the value of the user name
Int i1=123;//password value
If (password==i1 & amp; & Name=={str1)
System. The out. Println (" welcome, green ");
} else {
System. The out. Println (" I'm sorry, you are not a green ");
}
Why is obtained from the keyboard "green" and "green" in the str1 don't match
CodePudding user response:
Java==in addition to basic data types use to judge, objects are equal with the equals method to judgeCodePudding user response:
==symbol, if both sides are object references, judgment is the address of the memory object, rather than the content of the object,If you want to determine the content of the object, please call the equals method to judgment,
Two the "green" word does not match, because in memory, the name and str1 referenced objects, their address is not the same, they take up a different memory space,
Memory address is the same, and, objects, content is the same, are the two concepts,
Objects of the same address, it is the same object, so the contents will be the same,
Different address of the object, content can be the same, also can not the same, whether the address is the same, use the==symbol; Whether the content is the same, call the equals method,