• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Doubt in enthuware Mock exam question - String and StringBuilder

 
Greenhorn
Posts: 27
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23907
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 145
4
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic