我有这段代码,为什么 bo3= st1 == st2 给出 true ?
它们在内存中的位置是不是不同,通过==我们比较位置是否相同!
另外,如果有什么不对的,请指出我。感谢你们。
public static void main(String[] args) {
String st1 = "HELLO";
String st2 = "HELLO";
String st3 = "hello";
int comp1 = st1.compareTo(st2); // equal 0
int comp2 = st1.compareTo(st3); // -32
boolean bo1 = st1.equals(st2); //true
boolean bo2 = st1.equals(st3); // false , if ignoreCase will be true
boolean bo3 = st1==st2; //true ??????????? should not be true
boolean bo4 = st1 == st3; //false
int ind1 = st1.indexOf("L"); // 2
int ind2 = st1.lastIndexOf("L"); // 3
boolean con = st1.contains("LLO"); //true
System.out.println(bo3);
}
当我输入“玛丽”时,我有另一个代码,结果:同名而不等于
public static void main(String [] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("What is your name? ");
String name = keyboard.next();
if (name.equals("Mary"))
System.out.print("Same name");
else
System.out.print("Different name");
if (name == "Mary")
System.out.println("equal");
else
System.out.println("not equal");
}