String literal1 = "java";
String object = new String("java");
String literal2 = "java";
System.out.println("result 1 = " + (literal1 == object) );
System.out.println("result 2 = " + literal1.equals(object));
System.out.println("result 3 = " + literal1 == object);
System.out.println("result 4 = " + literal1.equals(object));
System.out.println("result 5 = " + literal1 == literal2);
System.out.println("result 6 = " + literal1.equals(literal2));
预期产量
result 1 = false
result 2 = true
result 3 = false
result 4 = true
result 5 = false
result 6 = true
获得的输出
result 1 = false
result 2 = true
false
result 4 = true
false
result 6 = true
当这条线
System.out.println(“结果5 =” +文字1 ==文字2);
更改为
System.out.println(“ result 5 =” +(literal1 == literal2));
输出
result 5 = true
谁能解释为什么会这样吗?
莫回无
慕工程0101907
繁星coding
相关分类