This content has been marked as final. Show 5 replies
No, you changed 'input' to refer to a different String.
You did not change any String. input is just a variable, just a reference to some string. In the line
you change this reference to the new String object on the right side. The variable "is" not the object it represents.
input = input.toLowerCase();
I am still confused.... ( is it not that I changed input ??)
Getting to grips with the difference between references and objects is pretty important.
If you see
it is clear that you have two references (var1 and var2) , each pointing to different objects. Now consider
A var1 = new A(); A var2 = new A();
you have two references but only one object (both references point to the same object).
A var1 = new A(); A var2 = var1;
think of this a bit like
input = input.toLowerCase()
Hopefully you get the idea - toLowerCase creates a new string initialised with the data; it does not alter the initial string which is why
input = new String( initialised with input as lower case);
on its own does not achieve anything - you have to assign the return to something (even if the something is the original reference) otherwise the new String object just gets lost.
As you have mentioned...i was trying earlier input.toLowerCase() and found the String input was not being altered...
now i am clear...Thanks a lot Smith :)