i know what is difference '==' with 'equals' and JVM String recycle in String Constant pool!
but this is some weird.
String m1 = "aa".getClass().getSimpleName();
String m2 = "String";
System.out.println("m1 = " + m1); // String
System.out.println("m2 = " + m2); // String
System.out.println(m1 == m2); // false... !!!
I wanted to see if constants in other classes could not be recycled, so I used strings in other classes as follows.
public class GenerateString {
public String foo() {
return "String";
}
}
public class Client {
public static void main(String[] args) {
GenerateString generateString = new GenerateString();
String m1 = generateString.foo();
String m2 = "String";
System.out.println("m1 = " + m1); // String
System.out.println("m2 = " + m2); // String
System.out.println(m1 == m2); // true
}
}
This, of course, caused true as expected. Why does false come out when I get the name of the class with reflection?