-3

I wrote a simple code:

public class Maintest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String key = "12345testabcd";
        System.out.println(key.getBytes());
        System.out.println(key.getBytes());
    }

}

when I run it the two print results are different.Why?Is this println related?Thx.

kk luo
  • 549
  • 1
  • 9
  • 22
  • 2
    Does this answer your question? [What's the simplest way to print a Java array?](https://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array) – knittl Mar 24 '23 at 20:17
  • You aren't printing the contents of the byte array, you're printing the identity hash code, which is sort of like its memory location. – Louis Wasserman Mar 24 '23 at 21:09

1 Answers1

3

You are seeing the default toString for each byte[] which is different each time as you get different byte[] instances back for each key.getBytes() call. Use this to see the contents of each byte array, they will have same values for same value of key:

System.out.println(Arrays.toString(key.getBytes()));
DuncG
  • 12,137
  • 2
  • 21
  • 33