equals和==到底应当怎么区别?
以下三组结果分别怎么解释,谢谢
class Demo
{
public static void main(String[] args)
{
Person p1=new Person("zhansan",20);
Person p2=new Person("zhansan",20);
System.out.println(p1.equals(p2)+"---"+(p1==p2));//false false
String s1=new String("lisi");
String s2=new String("lisi");
System.out.println(s1.equals(s2)+"---"+(s1==s2));//true false
String s3="lisi";
System.out.println(s1.equals(s3)+"---"+(s1==s3));//true false
}
}
class Person
{
private String name;
private int age;
Person(String name,int age)
{
this.name=name;
this.age=age;
}
}
|