java学习,根据要求创建相同(存储地址相同)的对象 10
写一个Person类,要有id,name,age属性,靠id来判断是否是同一个人。Personperson1=newPerson(1,"Tom",21);Personpe...
写一个Person类,要有id,name,age属性,靠id来判断是否是同一个人。
Person person1 = new Person(1, "Tom", 21);
Person person2 = new Person(1, "Tom", 21);
即person1的存储地址与person2的存储地址相同。
刚开始学java,网上找了很久没有相关资料,希望能获得指点! 展开
Person person1 = new Person(1, "Tom", 21);
Person person2 = new Person(1, "Tom", 21);
即person1的存储地址与person2的存储地址相同。
刚开始学java,网上找了很久没有相关资料,希望能获得指点! 展开
1个回答
展开全部
第一步骤:解决方式直接复制就可以了。
public class Test6 {
public static void main(String[] args) {
Person person1 = new Person(1, "Tom", 21);
Person person2 = person1;
System.out.println(person1.hashCode()+" "+person2.hashCode());
System.out.println(person1.equals(person2));
System.out.println(person2 == person1);
}
}
第二步骤:首选你要知道在java中使用new关键字创建一个对象时就会重新申请一个新的内存地址,当然基本数据类型如int新建的内容会放到静态池中,在创建一样的内容时会将新的变量指向已有的地址,不会新建。当时如果使用封装类的话也会新建地址:
public static void main(String[] args) {
int a = 55;
int b = 55;
System.out.println(a == b);
Integer it1 = new Integer(54);
Integer it2 = new Integer(54);
System.out.println(it1.hashCode() + "--" + it2.hashCode());
System.out.println(it1 == it2);
}
第三步骤:在我们常用的比较中==比较的是地址,equals比较的是值针对的是字符串Strign,而对象使用==比较的是地址equals比较的是地址转化的字符串就算内容相等返回也不一定是true
第四步骤:获取对象的地址。
package test;
import java.lang.reflect.Field;
import sun.misc.Unsafe;
public class GetObjectpath {
static final Unsafe unsafe = getUnsafe();
static final boolean is64bit = true;
public static void printAddresses(String label, Object... objects) {
System.out.print(label + ": 0x");
long last = 0;
int offset = unsafe.arrayBaseOffset(objects.getClass());
int scale = unsafe.arrayIndexScale(objects.getClass());
switch (scale) {
case 4:
long factor = is64bit ? 8 : 1;
final long i1 = (unsafe.getInt(objects, offset) & 0xFFFFFFFFL) * factor;
System.out.print(Long.toHexString(i1));
last = i1;
for (int i = 1; i < objects.length; i++) {
final long i2 = (unsafe.getInt(objects, offset + i * 4) & 0xFFFFFFFFL) * factor;
if (i2 > last)
System.out.print(", +" + Long.toHexString(i2 - last));
else
System.out.print(", -" + Long.toHexString( last - i2));
last = i2;
}
break;
case 8:
throw new AssertionError("Not supported");
}
System.out.println();
}
private static Unsafe getUnsafe() {
try {
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
return (Unsafe) theUnsafe.get(null);
} catch (Exception e) {
throw new AssertionError(e);
}
}
}
第五步骤:测试。
直接使用new创建对象地址是不同的,只有复制才能是地址一致
package test;
public class Test5 {
public static void main(String[] args) {
Person person1 = new Person(1, "Tom", 21);
Person person2 = new Person(1, "Tom", 21);
System.out.println(person1.hashCode()+" "+person2.hashCode());
System.out.println(person1.equals(person2));
System.out.println(person2 == person1);
GetObjectpath.printAddresses("person1", person1);
GetObjectpath.printAddresses("person2", person2);
person2 = person1;
System.out.println("--------------------------------------------");
System.out.println(person1.hashCode()+" "+person2.hashCode());
System.out.println(person1.equals(person2));
System.out.println(person2 == person1);
GetObjectpath.printAddresses("person1", person1);
GetObjectpath.printAddresses("person2", person2);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询