aspose file tools*
The moose likes Java in General and the fly likes diff between pass by reference or pass by value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "diff between pass by reference or pass by value" Watch "diff between pass by reference or pass by value" New topic
Author

diff between pass by reference or pass by value

rajareddy annavaarm
Ranch Hand

Joined: Mar 15, 2007
Posts: 96
diff between pass by reference or pass by value?
which one java uses?why?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42267
    
  64
The FAQ to the rescue: CallByReferenceVsCallByValue


Ping & DNS - my free Android networking tools app
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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 ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42267
    
  64
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

Joined: Oct 30, 2001
Posts: 1970
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.
Jaap v Hengstum
Greenhorn

Joined: Apr 25, 2007
Posts: 6
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
Marshal

Joined: Mar 22, 2005
Posts: 42267
    
  64
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.
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
Maybe references should be renamed to reflect what they are to avoid confusing java references to pass by reference: implicit pointers.


"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

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."


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: diff between pass by reference or pass by value