Java,=,equalsの違い
1652 ワード
public class A{
public static void main(String[] args) {
Integer a = 12;
Integer b = 12; // a,b a,b
System.out.println(a == b);// == : ,
System.out.println(a.equals(b));// equals :
}
}
// :
//true
//true
public class A{
public static void main(String[] args) {
Integer a = new Integer(12);
Integer b = new Integer(12); // Integer a,b new ,
System.out.println("a= "+a+" ,b= "+ b);
System.out.println(a == b);// == : ,
System.out.println(a.equals(b));// equals :
}
}
// :
//a= 12 ,b= 12
//false
//true
public class A{
public static void main(String[] args) {
// 127, ==
// , == != , equal() 。
Integer a = 128;
Integer b = 128; // Integer a,b new ,
System.out.println("a= "+a+" ,b= "+ b);
System.out.println(a == b);// == : ,
System.out.println(a.equals(b));// equals :
}
}
// :
//a= 128 ,b= 128
//false
//true
public class A{
public static void main(String[] args) {
String a=new String ("a");
String b=new String ("a");
System.out.println("a= "+a+" ,b= "+ b);
System.out.println(a == b);// == : ,
System.out.println(a.equals(b));// equals :
}
}
// :
//a= a ,b= a
//false
//true
参照先:http://blog.csdn.net/ma451152002/article/details/9076793