public class A {
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
//System.out.println( ClassLoader.getSystemClassLoader().toString());
Unsafe unsafe = UnsafeUtil.getUnsafe();
Person person = new Person();
long offset = unsafe.objectFieldOffset(person.getClass().getDeclaredField("name1"));
System.out.println(offset); // 16
person.setName1("fasfsa321312");
long offset1 = unsafe.objectFieldOffset(person.getClass().getDeclaredField("name1"));
System.out.println(offset1);
}
private static class Person{
// private int a;
private String name;
private String name1;
// private static int c;
// private int b;
public void setName(String name){
this.name = name;
}
public String getName(String name){
return this.name;
}
public void setName1(String name){
this.name1 = name;
}
public String getName1(String name){
return this.name1;
}
}
private static class UnsafeUtil {
private static Unsafe getUnsafe() throws NoSuchFieldException, IllegalAccessException {
Field field = Unsafe.class.getDeclaredField("theUnsafe");
field.setAccessible(true);
return (Unsafe) field.get(null);
}
}
}
output result is
16
16
what I want to know is why the memory address does not change when the string changes? Bosses help answer
.