• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java uses Pass By Value or Reference

 
Vijayagopalan Raveendran
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.!!!
 
Calvin Kong
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pass by value.
 
Nischal Tanna
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)
 
Tony Morris
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
 
Marcus Green
arch rival
Rancher
Posts: 2813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.jchq.net/certkey/0504certkey.htm
 
Jeroen Wenting
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

 
Dibbo Khan
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.
 
Timmy Marks
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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic