About Question enthuware.ocajp.i.v7.2.1284 :
Moderator: admin
-
- Posts: 30
- Joined: Fri Dec 07, 2012 6:09 pm
- Contact:
About Question enthuware.ocajp.i.v7.2.1284 :
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().
-
- Site Admin
- Posts: 10056
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1284 :
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.
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.
-
- Posts: 25
- Joined: Thu Jan 10, 2013 8:45 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1284 :
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?
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.
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.
-
- Site Admin
- Posts: 10056
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1284 :
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.
-
- Posts: 12
- Joined: Thu Aug 01, 2013 3:44 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1284 :
If we made replaceString(String s) method to return a value s like this:
and then in a main method did this:
then it would create lavajavac output. I also answered this question wrong but by experimenting with the code I found out the solution.
Code: Select all
static String replaceString(String s) {
s = s.replace('j', 'l');
return s;
}
Code: Select all
s1 = replaceString(s1);
-
- Posts: 35
- Joined: Sat Nov 25, 2017 4:13 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1284 :
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?
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?
-
- Site Admin
- Posts: 10056
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1284 :
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.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?
If you like our products and services, please help us by posting your review here.
-
- Posts: 35
- Joined: Sat Nov 25, 2017 4:13 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1284 :
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");
}
}
Last edited by admin on Fri Dec 15, 2017 9:13 pm, edited 1 time in total.
Reason: Please put code inside [code] [/code]
Reason: Please put code inside [code] [/code]
-
- Site Admin
- Posts: 10056
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1284 :
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.
-
- Posts: 85
- Joined: Mon Dec 24, 2018 6:24 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1284 :
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?
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?
-
- Site Admin
- Posts: 10056
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1284 :
Yes.
No, we say String is immutable because it is immutable. Immutability has nothing to do with string pool.Thats why we say String is immutable?
Correct.Now s1 just point to a new String Object but original one will not be garbage collected?
If you like our products and services, please help us by posting your review here.
-
- Posts: 85
- Joined: Mon Dec 24, 2018 6:24 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1284 :
Thanks. Can you simply explain Immutability please?
-
- Site Admin
- Posts: 10056
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1284 :
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.
-
- Posts: 85
- Joined: Mon Dec 24, 2018 6:24 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1284 :
Thanks, that helps a lot.
-
- Posts: 4
- Joined: Thu Sep 14, 2017 6:41 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1284 :
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.
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.
-
- Site Admin
- Posts: 10056
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1284 :
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.
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.
-
- Posts: 4
- Joined: Thu Sep 14, 2017 6:41 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1284 :
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.
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.
-
- Site Admin
- Posts: 10056
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1284 :
Correct.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 48 guests