• Post Reply Bookmark Topic Watch Topic
  • New Topic

Pass by Value/ Pass by reference  RSS feed

 
Gaurav x Jain
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ques. Is java is Pass by refernce or Pass by Value?

I got so many answers on net but they are confusing. Please help me
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the faq.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gaurav x Jain wrote: . . . Is java is Pass by refernce or Pass by Value? . . .
Pass by value. Full stop. If you have found something saying, "primitives are passed by value and objects are passed by reference," it is mistaken.
 
Ralph Cook
Ranch Hand
Posts: 479
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the article at http://www.javaworld.com/javaqa/2000-05/03-qa-0526-pass.html does a good job of explaining things.

Java passes by value, but since the value of an object is a reference to that object, then a called procedure can change the internal values of an object passed to it. So if you are thinking of "pass by value" to mean "called procedure cannot change it", it's a little confusing. A called procedure can change things about an object passed to it, but it cannot change the object reference.

As I say, the above article explains more fully, with several good examples.

rc
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java only supports pass-by-value.

Note that variables of non-primitive types are references in Java. When you pass such a value to a method, you're passing a reference by value - the reference is copied, but not the object that the reference refers to. Passing a reference by value is not the same as passing by reference.

Have a look at the JavaRanch Campfile story Pass-by-Value Please.

Ralph Cook wrote:... but since the value of an object is a reference to that object, ...

That's not accurate. You mean: The value of a (non-primitive) variable is a reference to an object.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!