Can someone please explain to me why this code print only false (Not Both are equal: false)
String one = "length: 10";
String two = "length: " + one.length();
System.out.println("Both are equal:" + one == two);
Can someone please explain to me why this code print only false (Not Both are equal: false)
String one = "length: 10";
String two = "length: " + one.length();
System.out.println("Both are equal:" + one == two);
System.out.println("Both are equal:" + one == two);
is evaluated as
System.out.println(("Both are equal:" + one) == two);
i.e. first one
is appended to "Both are equal:" , which results in the String
"Both are equal:length: 10", and then that String
is compared to two
, which results in false
, so only false
is printed.
What you want is
System.out.println("Both are equal:" + (one == two));
Just put the brackets here:
System.out.println("Both are equal:" + (one == two));