aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Java uses Pass By Value or Reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Java uses Pass By Value or Reference " Watch "Java uses Pass By Value or Reference " New topic
Author

Java uses Pass By Value or Reference

Vijayagopalan Raveendran
Ranch Hand

Joined: Apr 18, 2005
Posts: 41
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.!!!


Vijay<br />SCJP 1.5 / SCWCD 1.4 / SCDJWS 1.4 / SCBCD 5/ SCEA 5 (P1)/ SOA 667
Calvin Kong
Ranch Hand

Joined: Jul 02, 2003
Posts: 37
Pass by value.


Cal<br />SCJP,SCWCD,SCBCD
Nischal Tanna
Ranch Hand

Joined: Aug 19, 2003
Posts: 182
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)


Thnx
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Java strictly passes all types by value.

Here is some more explanation:
http://qa.jtiger.org/GetQAndA.action?qids=37&showAnswers=true


Tony Morris
Java Q&A (FAQ, Trivia)
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
http://www.jchq.net/certkey/0504certkey.htm


SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
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.


42
Nischal Tanna
Ranch Hand

Joined: Aug 19, 2003
Posts: 182
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

Joined: Dec 19, 2004
Posts: 147
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.


MCPD (Enterprise Application Developer, Windows Developer, Web Developer - .NET 2.0), MCTS (Windows Apps, Web Apps and Disbributed Applications - .NET 2.0), MCITP (Database Developer & Business Intelligence Developer - SQL Server 2005), MCAD, MSCD.net, SCJP 5, SCWCD 1.4, SCBCD, SCMAD, SCDJWS, SCJA
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

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

Joined: Oct 12, 2000
Posts: 5093
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

Joined: Dec 01, 2003
Posts: 226
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java uses Pass By Value or Reference