Domo is Fugito.
This time is a little memorandum.
in conclusion,
"** == operator determines identity and The equals method determines identity and equivalence ** "
There seems to be a difference.
"Whether or not they are the same instance".
"The values stored in the instance are the same Whether or not it is. "
An example is shown below.
public class Example{
public static void main(String[] args){
String s1 = "012";
String s2 = new StringBuilder("012").toString();
if(s1 == s2){
System.out.println("true");
}else{
System.out.println("false");
}
if(s1.equals(s2)){
System.out.println("true");
}else{
System.out.println("false");
}
}
}
"s1 == s2" means "** whether s1 and s2 are the same instance **" It will be judged. In the above program, s1 and s2 are independent of each other. Created as an instance. So the same instance I can't say that, so ** false ** is returned. By the way, as follows When rewritten, s1 == s2 becomes true.
String s1 = "012";
String s2 = s1;
On the other hand, the equals method is the same in the method by the == operator first. After determining the sex, the equivalence is determined. Here, ** for s1 and s2 Since the same string literal "012" is stored **, s1.equals (s2) Is ** true **.
-The == operator determines ** identity ** -The equals method determines ** identity and equivalence ** -Identity judgment is "** Is it the same instance " -Judgment of equivalence is " stored in the instance Whether the values are the same ** "
Recommended Posts