我有一节课:
package com.test;
public class TestA {
public static final String TEST = "饕餮ABCDEFG";
public TestA() {
System.out.println(TEST.hashCode());
}
}
同一个类(没有包,把它放在E:,javac Test.java中,得到Test.class):
public class TestA {
public static final String TEST = "饕餮ABCDEFG";
public TestA() {
System.out.println(TEST.hashCode());
}
}
测试类:
package com.test;
import java.net.URL;
import java.net.URLClassLoader;
public class Test3 {
public static void main(String[] args) throws Exception {
URLClassLoader loaderA = new URLClassLoader(new URL[]{new URL("file:E:/")});
Class clazzA = loaderA.loadClass("TestA");
clazzA.newInstance();
TestA testA = new TestA();
}
}
输出:
250218913
1111280555
问题:为什么会这样?我认为字符串“饕餮ABCDEFG”具有相同的地址,因为常量池。 enter image description here