Page 1 of 1
About Question com.enthuware.ets.scjp.v6.2.565 :
Posted: Tue Jul 26, 2011 12:30 pm
by ETS User
There is a conflict... because String class has String replace(char old, char new) method... therefore it should be replacing.... The output should be lavajavac....
But as String object is immutable the answer is correct.... if such questions come in exam how do we answer?
Re: About Question com.enthuware.ets.scjp.v6.2.565 :
Posted: Tue Jul 26, 2011 4:01 pm
by admin
Difference of immutability between String and StringBuffer/Builder is very basic and you should learn its implications. So in the exam you have to answer the question depending of what class is used, String or StringBuffer/Builder.
Re: About Question com.enthuware.ets.scjp.v6.2.565 :
Posted: Wed Jul 27, 2011 12:42 pm
by Guest
OK!! as replace() returns a new string object rather than modifiying the original one... therefore the ans shud be lavajavac right?
Re: About Question com.enthuware.ets.scjp.v6.2.565 :
Posted: Wed Jul 27, 2011 1:10 pm
by admin
In replaceString(String s) method, you are changing the method parameter s and not the original parameter s1. s1 will still point to the same String object, which contains "java".
Within the replaceString method, s = s.replace('j', 'l') will cause s to point to a new string containing "lava", but this doesn't change s1.
You should read about pass by value and pass by reference in java. It is a very important concept.
HTH,
Paul.
Re: About Question com.enthuware.ets.scjp.v6.2.565 :
Posted: Wed Jul 27, 2011 3:26 pm
by Guest
Uff!!! finally understood thanks Paul!!!