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

Pass by Reference

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
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
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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
output for above code is i=1, intArr[0] = 2
 
Roel De Nijs
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
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?
 
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah i have read it and understud as well thank you so much
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic