I have this code, why bo3= st1 == st2 gives true ?
are not they in different position in the memory, and by == we compare the position whether it is the same or not !
Also if there is something wrong, please indicate me. Thank you guys.
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);
}
While I have another code when I enter "Mary", the result: Same name and not equal
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");
}