String string1 = \"Hi there\";
String string2 = \"Hi\";
String string3 = \"Hi\";
System.out.println(string1.substring(0, string2.length()) == string2); //1
System.o
Several things going on there.
When you declare two strings to be equal "Hi", java optimizes it to refer to the same string object (so it doesn't have to store the same string twice).
"==" compares references. When in line 1 you use substring, the created string object has a different reference than string2.
In line 6, I imagine substring(0) has an optimization where it knows it can just return the original string, thus those references are equal.