- //Person类
- class Person
- {
- String name;
- int age;
- Person(String name, int age){
- this.name = name;
- this.age = age;
- }
- }
- class Test
- {
- public static void main(String[] args)
- {
- String s1 = "ab";
- String s2 = "ab";
- Person p1 = new Person("张三",32);
- Person p2 = new Person("李四",24);
- Person p3 = p1;
- System.out.println(s1 == s2);
- System.out.println(p1.equals(p2));
- System.out.println(p1.equals(p3));
- }
- }
复制代码 比较对象,equals是比较了地址,但记得貌似在String类里面equals是比较内容,双等号是比较地址是吗?这里是s1和s2是指向同一个内存空间吗
|