aspose file tools*
The moose likes Beginning Java and the fly likes Pass by Reference in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Pass by Reference in Java" Watch "Pass by Reference in Java" New topic
Author

Pass by Reference in Java

Nagaraju Mulinti
Ranch Hand

Joined: Aug 10, 2008
Posts: 37
Hi All,

How the java use the concept of pass by reference when we use code like in below. I think java only support pass by reference only, not pass by value.but how it handle the scenarios like below.





Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Check this FAQ.


[My Blog]
All roads lead to JavaRanch
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

Nagaraju Mulinti wrote:I think java only support pass by reference only, not pass by value.

No, exactly the contrary - Java always passes arguments by value. Java does not support pass-by-reference.

For primitive values, this is easy to understand - when you pass a primitive value to a method, it just passes the value, and not a reference to the variable that holds the value.

Non-primitive values are references to objects. The thing that makes it confusing is that you have to understand that those references are passed by value. In other words, the reference to the object is passed by value - that's not the same as pass-by-reference (which implies that there's a reference passed to the variable itself).

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Lots of people get confused about this; look at this thread.
Nagaraju Mulinti
Ranch Hand

Joined: Aug 10, 2008
Posts: 37
So, we can say primitives are by value and non-primitives are by reference..
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

No. Primitives are passed by value. References are also passed by value. That means you can change the object the reference refers to, but you can't change the reference itself:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
I think it would have been clearer if you had said you can "change the state of the object".
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

True.
Kr Manish
Ranch Hand

Joined: Jul 30, 2010
Posts: 138
I too used to get so confused on this topic. The *only* thing to keep in mind in this topic is when OBJECTS are passed (as opposed to primitives like int), we are *not* passing the object BUT a reference (or pointer if you will) and we pass this reference using pass by value. So now we have *TWO* copies of references, BUT only *ONE* single copy of the object. This makes sense, because objects are made using "new" operator, and since we have created the object just once we should have just one copy of the object.
The called method can now use *its* copy of the reference (which was passed by value) to alter the values of this object, and THIS phenomenon we confusingly perceive as passing by reference.


You know what I am saying ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Nice little explanation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pass by Reference in Java
 
Similar Threads
Callback references
When "\'something'" or "\\'something'" will not compile
Java - Pass By Value /Pass By Reference
Passing by value vs. reference
Simple thread question