问题描述
public static void main(String[] args){String a = "1";String b = "1";System.out.println("a == a -> " + a == a);System.out.println("a == b -> " + a == b);System.out.println(a.equals(b));System.out.println(a.hashCode() == b.hashCode());}猜一猜运行结果!!!! 问题补充:gds718 写道
解决方案
运行了一下,大惊,结果不是String的问题...这样写就符合逻辑了public static void main(String[] args){String a = "1";String b = "1";System.out.println("a == a -> " + (a == a));System.out.println("a == b -> " + (a == b));System.out.println(a.equals(b));System.out.println(a.hashCode() == b.hashCode());}
时间: 2024-10-27 04:40:08