• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Reg garbage collection

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. public class X {
2. public static void main(String [] args) {
3. X x = new X();
4. X x2 = m1(x);
5. X x4 = new X();
6. x2 = x4;
7. doComplexStuff();
8. }
9. static X m1(X mx) {
10. mx = new X();
11. return mx;
12. }
13. }

Since java uses pass by reference for objects the reference passed to the method m1() will refer to the sameinstance x declared at line 3. Inside the method we assign the reference to a new object thus removing the refernce to the previously pointing object. Can you tell me how many objects are eligible for garbage collection. In the book its given as the object created at line 4 is eligible for garbage collection but i think its line 3 onject that is eligible for garbag collection. can i gert a clarification
Thanks
 
Pradeep Kumar
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is my undersatndin to the above program

1.After line 3 this will happen
_____
| |
| |
| |----->x
| |
|_____|

2. After line 4 this will happen
_____
| |
| |
| |--//--->x
| | |
|_____| |
|
_____ |
| | |
| |---|
| |----->x2
| |
|_____|

3. After line 6 this will happen
_____
| |
| |
| |---//-->x
| | |
|_____| |
|
_____ |
| | |
| |---|
| |----->x2
| | |
|_____| | // Not furthur referenced
//
_______ |
| | |
| |----
| |----> x4
| |
|_____|

So as x2 and x4 both refer to same x4 boject and x doies not have a reference the object created at line 3 is eligible for garbage collection. Is my understanding correct.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I don't understand your diagrams...

What object does x reference after line 4 is executed, in your opinion?
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you are right Pradeep. Java does pass by reference only.

But inside the method m1, the received object is stored in a reference variable 'mx' and that object is local to that method. You are reassigning the local 'mx' variable to point to a new object and NOT the actual object being passed from your main() method.


...In the book its given as the object created at line 4 is eligible for garbage collection but i think its line 3 onject that is eligible for garbag collection. can i gert a clarification



Still, the object created in line 3 is being referred by the reference variable 'x'. Only the object being pointed by the reference variable 'x2' is lost since 'x2' is being made point to the same object as that of 'x4'.

Does that help Pradeep?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Raghavan Muthu:
[QB]Yes you are right Pradeep. Java does pass by reference only.QB]



No !! Both you and Pradeep are wrong. Java does pass by value - always.
However, you have to remember that Java does not pass objects to methods. It passes references to objects.

Here, x is a reference to an X object. When you call m1, you are passing a copy of this reference to the method. The original x cannot be changed in m1, because only a copy of it was passed.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


..No !! Both you and Pradeep are wrong. Java does pass by value - always.



It was an idiotic typo mistake. By thinking that i am confirming for 'pass by value' i typed the reverse.

Thank you Joanne Neal !
 
Pradeep Kumar
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But java uses call by reference when we pass objects to the method. i.e, when we pass an objec to a method it internally passes call by vlaue but the value is just a bit pattern and not the object. so the passed object will still refer to the object for ex

1 Object o = "toBePassed";

2 doStuff(o);

in the doStuff method

3 doStuff(Object obj){
4 Object o = obj;
}

the object obj refers to the same object created at line 1 also o created referenced at line 4 still points to the same object(created at line1) is this right?
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


..the object obj refers to the same object created at line 1 also o created referenced at line 4 still points to the same object(created at line1) is this right?



Yes. the bit patterns to reach the object is NOT changed yet. so they are the same.
 
Now I am super curious what sports would be like if we allowed drugs and tiny ads.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic