About Question enthuware.ocajp.i.v7.2.1284 :

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

Moderator: admin

Post Reply
ksnortum
Posts: 30
Joined: Fri Dec 07, 2012 6:09 pm
Contact:

About Question enthuware.ocajp.i.v7.2.1284 :

Post by ksnortum »

Something that isn't touched on in the explanation but that I think is important is that although Java is always "pass by value", because it is the reference to s2 that is passed, the object pointed to by s2 will be changed by replaceStringBuilder().

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

Re: About Question enthuware.ocajp.i.v7.2.1284 :

Post by admin »

Pass by value aspect is covered in other questions.
In this question, in both the cases the copy of references is passed. So had String not been immutable, it should have been changed as well. Therefore, the main point in this question is the immutability of the Strings.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Ambiorix
Posts: 25
Joined: Thu Jan 10, 2013 8:45 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1284 :

Post by Ambiorix »

I've read the pass by value/reference section in the study guide and understand that there will be an s1 and s reference pointing to the same String object.

My question is, what happens when you execute the replace statement?

Code: Select all

s = s.replace('j', 'l');


Since strings are immutable, it can't update the existing object. Does it therefore create a second object on the heap?

It outputs 'lava' if I insert a println(s) after the replace, so I assume that it does.

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

Re: About Question enthuware.ocajp.i.v7.2.1284 :

Post by admin »

Yes, it creates a new string object on the heap (In Java, all objects are created on the heap).
If you like our products and services, please help us by posting your review here.

adrian110288
Posts: 12
Joined: Thu Aug 01, 2013 3:44 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1284 :

Post by adrian110288 »

If we made replaceString(String s) method to return a value s like this:

Code: Select all

static String replaceString(String s) {
     s = s.replace('j', 'l');
     return s;
  }
and then in a main method did this:

Code: Select all

s1 = replaceString(s1);
then it would create lavajavac output. I also answered this question wrong but by experimenting with the code I found out the solution.

Rinkesh
Posts: 35
Joined: Sat Nov 25, 2017 4:13 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1284 :

Post by Rinkesh »

So no matter what you do in replaceString() method, the original String that was passed to it will not change.
If we return the s after replace method is performed and reassign the new value to replaceString(s1),it can be changed.So,the above statement maybe wrong?Am I missing something?

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

Re: About Question enthuware.ocajp.i.v7.2.1284 :

Post by admin »

Rinkesh wrote:So no matter what you do in replaceString() method, the original String that was passed to it will not change.
If we return the s after replace method is performed and reassign the new value to replaceString(s1),it can be changed.So,the above statement maybe wrong?Am I missing something?
Not really sure what you mean. Please put what you are saying in terms of code so that it is clear what exactly are you asking.
If you like our products and services, please help us by posting your review here.

Rinkesh
Posts: 35
Joined: Sat Nov 25, 2017 4:13 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1284 :

Post by Rinkesh »

Code: Select all

public class Sample{
   public static void main(String[] args)  {
     String s1 = new String("java");
     StringBuilder s2 = new StringBuilder("java");
     s1=replaceString(s1);
     replaceStringBuilder(s2);
     System.out.println(s1 + s2);
  }
  static String replaceString(String s) {
     s=s.replace('j', 'l');
     return s;
  }
  static void replaceStringBuilder(StringBuilder s) {
     s.append("c");
  }
}
I was trying to print lavajavac and this code worked.What I meant earlier was if we return and reassign the string,it can change.I am returning s here and reassigning it to replaceString(s1).
Last edited by admin on Fri Dec 15, 2017 9:13 pm, edited 1 time in total.
Reason: Please put code inside [code] [/code]

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

Re: About Question enthuware.ocajp.i.v7.2.1284 :

Post by admin »

Yes, s1 is not final so you can make it point to any String object. The String objects themselves don't change.
If you like our products and services, please help us by posting your review here.

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1284 :

Post by crazymind »

s1 = replaceString(s1);

Does original String object of s1 still exist in String pool? Thats why we say String is immutable? Now s1 just point to a new String Object but original one will not be garbage collected?

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

Re: About Question enthuware.ocajp.i.v7.2.1284 :

Post by admin »

crazymind wrote:
Sun Jan 06, 2019 6:15 pm
s1 = replaceString(s1);

Does original String object of s1 still exist in String pool?
Yes.
Thats why we say String is immutable?
No, we say String is immutable because it is immutable. Immutability has nothing to do with string pool.
Now s1 just point to a new String Object but original one will not be garbage collected?
Correct.
If you like our products and services, please help us by posting your review here.

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1284 :

Post by crazymind »

Thanks. Can you simply explain Immutability please?

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

Re: About Question enthuware.ocajp.i.v7.2.1284 :

Post by admin »

Immutability is explained nicely in this article: https://www.baeldung.com/java-immutable-object
If you like our products and services, please help us by posting your review here.

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1284 :

Post by crazymind »

Thanks, that helps a lot.

Belcheee
Posts: 4
Joined: Thu Sep 14, 2017 6:41 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1284 :

Post by Belcheee »

I'm missing something here, please could you assist?

replaceString method is passed the object reference s1 which currently points to literal String "java".
replaceString method takes the s1 object (literal String "java") and performs the .replace command which returns a new literal String "lava".
Because s in the method is being assigned to the result of the .replace call, how come the object s points to does not become the literal String "lava".

i.e. why is it not possible to change the value of s, when it is being assigned to a new literal String.
And if the value of s is changed, does that not then mean that the object s1 points to has now changed to literal String "lava".

Hope I've explained my question clearly - interested to know what I'm missing here.
Thanks.

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

Re: About Question enthuware.ocajp.i.v7.2.1284 :

Post by admin »

You have asked a very fundamental question about objects and references. I suggest you to go through these articles carefully:
https://javaranch.com/campfire/StoryPassBy.jsp
and https://www.journaldev.com/3884/java-is ... -reference
and then come back to this question.
Let me know if you still don't get it.
If you like our products and services, please help us by posting your review here.

Belcheee
Posts: 4
Joined: Thu Sep 14, 2017 6:41 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1284 :

Post by Belcheee »

Of course - it's pass by value. For some reason I was getting hung on it passing by reference. It was a long day..

To clarify for my own understanding:
replaceString method is passed the *value* of s1 (the object it points to - literal string "java" on the heap). In the method, s has the value "java" but then becomes "lava". But this doesn't change the literal string "java", it creates a new object "lava" instead. So "java" remains on the heap and s1 continues to point to it.

Is that correct? Thanks.

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

Re: About Question enthuware.ocajp.i.v7.2.1284 :

Post by admin »

Correct.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 59 guests