• 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

diff between pass by reference or pass by value

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
diff between pass by reference or pass by value?
which one java uses?why?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The FAQ to the rescue: CallByReferenceVsCallByValue
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, all in favour of FAQs, except when they're misleading...


In short in java, object references are passed by reference and primitive types are passed by value.



What? Java always passes by value.

Object references are passed by value. The callee gets a new copy of the object reference, which the callee may change without affecting the caller.

In Java you can't have variables that are objects. You can only have variables that are object references.

Some people like to say that, when passing an object reference by value to a method, they are effectively passing the object by reference. If you're one of those people, the FAQ could more correctly say: -


In short in java, objects are passed by reference and primitive types are passed by value.


[ May 03, 2007: Message edited by: Peter Chase ]
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Peter, since you have already worked your way through the FAQ, may I ask that you amend it to reflect your points?

(I admit that I hadn't actually read it, just pointed to it because I knew it was out there.)
 
Peter Chase
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I could do so, but how?

The FAQ looks fine to me, except for the one sentence mentioned in my previous post. I already included a suggested revision for that one sentence.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you look at the history, the FAQ entry used to say:

In short in java, object references are passed by value and primitive types are passed by value.



Then someone changed it the wrong way (I guess that happens when everyone can change a page). I reverted the text back to the original.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Peter and Jaap.

As an FYI, each FAQ has an "Edit" button at the bottom that allows the page to be changed. Generally we try to watch out for changes, but that's mostly to fix vandalism and remove spam.
 
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe references should be renamed to reflect what they are to avoid confusing java references to pass by reference: implicit pointers.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pass by reference, pass by value: Sun's Java Tutorial used to contain the same error, i.e. it said that primitives are passed by value and "objects are passed by reference".

I see that Sun has fortunately corrected the error now, this page in the tutorial now says:

"Primitive arguments, such as an int or a double, are passed into methods by value. This means that any changes to the values of the parameters exist only within the scope of the method. When the method returns, the parameters are gone and any changes to them are lost."

"Reference data type parameters, such as objects, are also passed into methods by value. This means that when the method returns, the passed-in reference still references the same object as before. However, the values of the object's fields can be changed in the method, if they have the proper access level."
 
reply
    Bookmark Topic Watch Topic
  • New Topic