What I read is that the function
toUpperCase
actually returns a new String object, and I did check the code inside Java.lang.String and it really creates a new string.
this is a code from Java.lang.string class, method toUpperCase
int resultOffset = 0;
char[] result = new char[len]; /* may grow */
This is my code now:
String s1 = new String("THAT");
String s2 = s1;
System.out.println(s1 == s2); // true
s1 = s1.toUpperCase();
System.out.println(s1 == s2); // true, how is that?
The first (s1 == s2) returns true and that is obvious. But how can the second one returns true as well? after the toUpperCase method, shouldn't s1 becomes a new String? while s2 still points to the old s1. Thus, the memory location for both of them should be not the same, and as ==
compares the memory locations. the result should be false.
I know that I am wrong in some point of my argument, but where is that point?