1

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

10
  • 6
    The constant pool shouldn't be involved at all since the hash code of a string is calculated from the underlying char array (so that 2 different instances still have the same hash code). What do you get when printing the char array itself in both cases (via TEST.toCharArray())? Commented Sep 17, 2018 at 14:59
  • java version "1.8.0_161" Commented Sep 17, 2018 at 15:01
  • @Thomas the key is 2 different string instances have same String literal and they have different hashCode, Commented Sep 17, 2018 at 15:04
  • 1
    The can't have the same value and a different hash code. As per the JavaDoc (which the source supports): 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. Commented Sep 17, 2018 at 15:06
  • 1
    I made a silly mistake! my os is windows10,the default encoding is gbk, and TestA is UTF-8 encoding, so we can't just use javac TestA.class,the right operation is javac -encoding utf-8 TestA.java Commented Sep 18, 2018 at 2:39

1 Answer 1

5

The program output gave the same hashcode for me.

  • 1111280555
  • 1111280555

The reason(s) why you are getting different hashcode

  1. There are some special character which you cannot see. You can identify by copying the text in hexed.it

  2. One file is stored as UTF8 and another as CP1652 or other encoding.

Sign up to request clarification or add additional context in comments.

1 Comment

you are right, use javac -encoding utf-8 TestA.java ,and output is same

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.