Page 1 of 1

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

Posted: Tue Dec 11, 2012 12:44 pm
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().

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

Posted: Tue Dec 11, 2012 7:11 pm
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.

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

Posted: Mon Mar 11, 2013 2:31 pm
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.

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

Posted: Mon Mar 11, 2013 2:35 pm
by admin
Yes, it creates a new string object on the heap (In Java, all objects are created on the heap).

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

Posted: Sat Aug 31, 2013 4:48 pm
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.

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

Posted: Thu Dec 14, 2017 7:34 pm
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?

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

Posted: Thu Dec 14, 2017 9:47 pm
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.

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

Posted: Fri Dec 15, 2017 8:56 pm
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).

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

Posted: Fri Dec 15, 2017 9:14 pm
by admin
Yes, s1 is not final so you can make it point to any String object. The String objects themselves don't change.

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

Posted: Sun Jan 06, 2019 6:15 pm
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?

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

Posted: Sun Jan 06, 2019 10:01 pm
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.

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

Posted: Mon Jan 07, 2019 3:00 pm
by crazymind
Thanks. Can you simply explain Immutability please?

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

Posted: Mon Jan 07, 2019 10:01 pm
by admin
Immutability is explained nicely in this article: https://www.baeldung.com/java-immutable-object

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

Posted: Mon Jan 07, 2019 11:42 pm
by crazymind
Thanks, that helps a lot.

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

Posted: Fri May 03, 2019 9:15 am
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.

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

Posted: Fri May 03, 2019 9:48 am
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.

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

Posted: Sun May 05, 2019 3:22 pm
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.

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

Posted: Sun May 05, 2019 6:25 pm
by admin
Correct.