• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Pass by Value/ Pass by reference

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out the faq.
 
Marshal
Posts: 80760
487
  • Likes 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 479
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic