• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pass by Reference

 
meghana chintanippu
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


the object created at line 1 shud be garbage collected after the call of makeThisNull in main as m is pass by reference so assigning pm to null will reflect m . but the answer given by enthuware tests is that its not garbage collected till the end of the program
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add following statement at the beginning and end of the makeItNull-method and see what's printed on the console (and be amazed )
 
meghana chintanippu
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but how come the below code is effecting the original array when only a copy of reference is send to the method
// class test


 
meghana chintanippu
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
output for above code is i=1, intArr[0] = 2
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you refuse each time to use code-tags to make (valid) code snippets (with proper indentation) more readible for other ranchers? It will also increase your chances to get an appropriate answer on your questions/doubts?
 
Heena Agarwal
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First you might want to indent your code and use code tags so it is readable.

Now coming to your question, if two variables are pointing to the same thing and one of them then starts pointing to something else, should it change what the other variable was pointing to also?

Here is a post that might interest you. That should answer your first question.

Ok, so before we take your second question, let's just get a confirmation from you that you understand the answer of the first one.

[ Edit : Ninja'd ]
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is some more code to play with ...
 
meghana chintanippu
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thankyou so much for your explantion
 
Heena Agarwal
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've probably already read the last comment in the code posted by E Armitage, but I'll just copy and post it again.


/**
* Notes: Java is pass by value always. Pass-by-value means that when you call a method, a copy of the value of each actual parameter is passed to the method. You can change that
* copy inside the method, but this will have no effect on the calling method.
* If the variable you are passing in is of reference type and you change the properties of the referred
* to object without pointing the reference to a new object then your changes will be visible in the calling method. The reference was still passed by value.
*/




 
meghana chintanippu
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah i have read it and understud as well thank you so much
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic