我想不通爲什麼調用getBytes()構造一個字符串兩次返回是不相等的()兩個字節數組:Java的String的getBytes nondetermism
final String aString = "hello world";
System.out.println(aString.getBytes());
System.out.println(aString.getBytes());
System.out.println(aString.getBytes());
打印:
[[email protected]
[[email protected]
[[email protected]
例如以下斷言總是失敗:
Assert.assertEquals(aString.getBytes(), aString.getBytes());
從doc,我沒想到的任何確定性!我錯過了什麼?
當轉換回字符串,結果是預期的,所以我最好的猜測是一些未初始化的填充位?
I.e.以下斷言總是通過:
Assert.assertEquals(new String(aString.getBytes()), new String(aString.getBytes()));
組合http://stackoverflow.com/questions/4479683/java-arrays-printing-out-weird-numbers-and-text和http://stackoverflow.com/questions/8777257/equals-vs-array -equals-in-java –