== 和!=
//: object/test.java package object;
import java.util.*; public class Test{
public static void main(String[] args)
{
Integer n1 = new Integer(47);
Integer n2 = new Integer(47);
System.out.println(n1 == n2);//output false
System.out.println(n1 != n2);//output true
}
}
//比较的是对象的引用
equals:equals 不适用于基本类型,基本类型用== !=
//: object/test.java package object;
import java.util.*; public class Test{
public static void main(String[] args)
{
int n1 = 100;
int n2 = 100;
System.out.println(n1==n2);//output false
}
}
equals
//: object/test.java package object;
import java.util.*; public class Test{
public static void main(String[] args)
{
Integer n1 = new Integer(47);
Integer n2 = new Integer(47);
System.out.println(n1.equals(n2));//output true
}
}
equals:假如自己创建自己的类 情况又不一样
//: object/test.java package object;
import java.util.*; class Value{
int i;
} public class Test{
public static void main(String[] args)
{
Value n1 = new Value();
Value n2 = new Value();
n1.i = n2.i = 100;
System.out.println(n1.equals(n2));//output false
}
}