Muthukris wrote:I am not sure if my methodology is correct, but I think it is and will give you the answer:
This is SCJP Question.... The answer given is 3...Buit according to my idea it will be 5... Because sting is immutable... So for each and every modification it will create object..
Did not know what to do about the System.out.println(s), but I believe no new string is created in there.
int i = 1; String s = "dgsg"; if ( s != s + "etre" ) i++; s+="etre"; if ( s != s.substring(1,3)) i++; s=s.substring(1,3); if ( s != s.toUpperCase()) i++; s = s.toUpperCase(); if ( s != s.toString()) i++; System.out.println(i);