【问题标题】:comparing two identical java byte arrays returns false [duplicate]比较两个相同的 java 字节数组返回 false [重复]
【发布时间】:2021-02-02 05:44:21
【问题描述】:
System.out.println("test".getBytes() == "test".getBytes() ? "same" : "diff");
System.out.println("test".getBytes().equals("test".getBytes()) ? "same" : "diff");

在这两行中都输出diff。它们是同一个东西,所以输出的不应该是same吗?

【问题讨论】:

  • 使用java.util.Arrays.equals("test".getBytes(), "test".getBytes())

标签: java


【解决方案1】:

试试这个。

System.out.println(Arrays.compare("test".getBytes(), "test".getBytes()) == 0 ? "same" : "diff");

【讨论】:

    猜你喜欢
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    • 2016-04-17
    • 1970-01-01
    • 2016-12-18
    • 1970-01-01
    • 2014-12-13
    相关资源
    最近更新 更多