I have a class:
package com.test;
public class TestA {
public static final String TEST = "饕餮ABCDEFG";
public TestA() {
System.out.println(TEST.hashCode());
}
}
The same class(with no package, put it in E:, javac Test.java, get Test.class):
public class TestA {
public static final String TEST = "饕餮ABCDEFG";
public TestA() {
System.out.println(TEST.hashCode());
}
}
The Test class:
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();
}
}
output:
250218913
1111280555
question: why this happened? i think the string "饕餮ABCDEFG" has the same address because constant pool. enter image description here
TEST.toCharArray())?The hash code for a String object is computed as s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] using int arithmetic, where s[i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation.- Thus again: what output do you get when printing the character array itself? That there might be non-printable characters in the literal - which you don't see.