From JCHQ: You have created two strings containing names. Thus String fname="John"; String lname="String" How can you go about changing these strings to take new values within the same block of code? 1) fname="Fred"; lname="Jones"; 2) String fname=new String("Fred"); String lname=new String("Jones"); 3) StringBuffer fname=new StringBuffer(fname); StringBuffer lname=new StringBuffer(lname);
4) None of the above
Why is the answer 4 not 1? Thanks, Jenny
posted 14 years ago
Because Strings are immutable. 1 doesn't change the String. It creates two brand new Strings! Tricky, huh?