Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pass by Reference in Java

 
Nagaraju Mulinti
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check this FAQ.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lots of people get confused about this; look at this thread.
 
Nagaraju Mulinti
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, we can say primitives are by value and non-primitives are by reference..
 
Rob Spoor
Sheriff
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it would have been clearer if you had said you can "change the state of the object".
 
Rob Spoor
Sheriff
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True.
 
Kr Manish
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice little explanation
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic