If I have:
Boolean isEqual = compare("Hello World");
Boolean compare(String c) {
String s = "Hello World";
return s == c;
}
In my experience, this returns true. Why?
If I have:
Boolean isEqual = compare("Hello World");
Boolean compare(String c) {
String s = "Hello World";
return s == c;
}
In my experience, this returns true. Why?
because its a string literal that has been interned, the same instance is used.
Use
new String("Hello World")
to make it false.Also asked many many many times before on here