测试class:
import org.openjdk.jol.vm.VM;
public class test {
public static void main(String[] args) {
String s1 = "abc";
String s2 = new String(s1);
System.out.println(s1.equals(s2));
System.out.println(s1==s2);
System.out.println("s1的内存地址:"+VM.current().addressOf(s1));
System.out.println("s2的内存地址:"+VM.current().addressOf(s2));
}
}
依赖
<dependency>
<groupId>org.openjdk.jol</groupId>
<artifactId>jol-core</artifactId>
<version>0.16</version>
<!-- 这里注意,从maven仓库复制过来的代码记得把下面这行注释掉 -->
<!--<scope>provided</scope>-->
</dependency>
输出
true
false
# WARNING: Unable to get Instrumentation. Dynamic Attach failed. You may add this JAR as -javaagent manually, or supply -Djdk.attach.allowAttachSelf
# WARNING: Unable to attach Serviceability Agent. sun.jvm.hotspot.memory.Universe.getNarrowOopBase()
s1的内存地址:30348633440
s2的内存地址:30348633488