About Question com.enthuware.ets.scjp.v6.2.565 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

About Question com.enthuware.ets.scjp.v6.2.565 :

Post 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?

admin
Site Admin
Posts: 10065
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.565 :

Post 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.
If you like our products and services, please help us by posting your review here.

Guest

Re: About Question com.enthuware.ets.scjp.v6.2.565 :

Post by Guest »

OK!! as replace() returns a new string object rather than modifiying the original one... therefore the ans shud be lavajavac right?

admin
Site Admin
Posts: 10065
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.565 :

Post 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.
If you like our products and services, please help us by posting your review here.

Guest

Re: About Question com.enthuware.ets.scjp.v6.2.565 :

Post by Guest »

Uff!!! finally understood thanks Paul!!!

Post Reply

Who is online

Users browsing this forum: No registered users and 111 guests