File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

diff between pass by reference or pass by value

 
rajareddy annavaarm
Ranch Hand
Posts: 96
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
diff between pass by reference or pass by value?
which one java uses?why?
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The FAQ to the rescue: CallByReferenceVsCallByValue
 
Peter Chase
Ranch Hand
Posts: 1970
  • 0
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Jaap v Hengstum
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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."
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic