Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in enthuware Mock exam question - String and StringBuilder

 
Indu Acharya
Greenhorn
Posts: 27
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Can someone explain me below code.

This question is from enthuware mock exam
My answer was s=null,sb=null
But the correct answer is s=aaa, sb=bbbaaabbb



My understanding of this code is, we are passing two object reference variables to method testRefs( ). Since this is pass-by-reference the called method can change the object the variable referred to.
Initial values of s="aaa" and sb = "bbb"


this makes str = "aaabbb";


correct me if I am wrong
Strings are immutable. In this case there are 2 string objects on heap - "aaa" and "aaabbb".

s refering to "aaa"
str refering to "aaabbb"

OR

intially s was refering to "aaa", when the above statement exceutes s refers to "aaabbb" and the first reference is abadoned



after this statement sb value is "bbbaaabbb"



this code assigning null to both reference variables.  when the control returns to main() method, println statement executes.


why the output in not null?
what is point of assigning null to str and sb reference variables?
 
Henry Wong
author
Marshal
Pie
Posts: 22111
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at this link...

https://coderanch.com/wiki/660293/Call-Reference-Call

Basically, Java is pass by value of the reference. So, changing the item being referenced will have an effect. Changing the reference itself won't.

Henry
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this topic you'll find another great explanation (with code snippets) about pass-by-value (and its consequences for reference variables).
 
Indu Acharya
Greenhorn
Posts: 27
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry and Roel.

I went through the links you have provided. My doubts are clear now.

Here is what I understood, correct me If I am wrong



Here we are changing the Object str refers to. So it did not affect the object s refers to.



Here we re not creating new Object, so it affects the original refernce variable sb.

If I change the above statement as below. then the original sb is unaffecetd.



the above concept applies to to these 2 as well

 
Narayana Bojja
Ranch Hand
Posts: 134
4
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Indu Acharya
Yes, your understanding is right. I want add some information to your explanation about making reference variables to null.

Generally , If you assign null to reference variable then it doesn't point to that object.

So here



After execution of the above statement, str will refer to new String object.

Now s is null, so it doesn't point that new String object. That new String object (not String object in main method) is eligible for garbage collection.

sb is still pointing to StringBuilder object created in main method. So StringBuilder object in main method has two reference variables sb, sb .



With the execution of the above statement, sb in testRefs method doesn't point to StringBuilder object in main method. But sb  in main method is still pointing to  StringBuilder object in main method. So it doesn't eligible for garbage collection because it has reference variable.

Hope this helps !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic