Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Java uses Pass By Value or Reference

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a doubt whether Java uses pass by refernce or pass by value.
Kindly any one can provide a insight on this?
Thanks in advance.!!!
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pass by value.
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not really.....

1. Primitives are passed by value
2. Objects (references) are passed by passing a copy of the reference (Not the actual reference !!! and hence only state changes on the copy will be reflected with the original reference)
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java strictly passes all types by value.

Here is some more explanation:
http://qa.jtiger.org/GetQAndA.action?qids=37&showAnswers=true
 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.jchq.net/certkey/0504certkey.htm
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Nischal Tanna:
Not really.....

1. Primitives are passed by value
2. Objects (references) are passed by passing a copy of the reference (Not the actual reference !!! and hence only state changes on the copy will be reflected with the original reference)



Java passes object references by value.
Thus while you can change the content of the referenced object you cannot change the reference (and thus replace the object with another one).

That's pass by value.
 
Nischal Tanna
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Jeroen Wenting:


Java passes object references by value.
Thus while you can change the content of the referenced object you cannot change the reference (and thus replace the object with another one).

That's pass by value.



Ok Ok... Obviously literally it is called pass by value ... where did i deny it?... but i wanted to explain technically rather than putting exact grammar

 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java passes object references by value,

Isn't that a meaningless answer, it will confuse the issue, you are still ultimately pointing to a reference on the managed heap if you are dealing with an object instead of a primitive type.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Java passes object references by value,

Isn't that a meaningless answer, it will confuse the issue...


No, and it will "unconfuse" the issue, respectively.
Not to mention that it is correct.
One more time: http://qa.jtiger.org/GetQAndA.action?qids=37&showAnswers=true
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Dibbo Khan:
Java passes object references by value,

Isn't that a meaningless answer, it will confuse the issue, you are still ultimately pointing to a reference on the managed heap if you are dealing with an object instead of a primitive type.



No.
What is passed is the reference, not the object.
Many people forget that, and then get highly frustrated when they try to do something like



and 1 is printed. If objects were passed by value as they expect 2 would be printed instead.
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
btw, a search of this forum with the words "Java Pass Value Reference" has 81 hits! The same search in Java in General (Beginner) resulted in 40 more! In 3 minutes, I found over 100 answers to this question on this board! Did we really need another one?
[ June 08, 2005: Message edited by: Timmy Marks ]
 
them good ole boys were drinking whiskey and rye singin' this'll be the day that I die. Drink tiny ad.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic