Popular Posts

Wednesday, October 16, 2013

Java - pass by value or pass by reference?

Many times we come across statements like 'In Java, Primitives are passed by value while Objects are passed by reference.'; which is not a correct statement.

Java is pass by value    -    Always!


For primitives, you pass a copy of the actual value.
For references to objects, you pass a copy of the reference (the remote control).
You never pass the object. All objects are stored on the heap. Always.
Below are few good links which provides explanation with examples:

http://www.javaranch.com/campfire/StoryPassBy.jsp
http://academic.regis.edu/dbahr/GeneralPages/IntroToProgramming/JavaPassByValue.htm
http://www.javadude.com/articles/passbyvalue.htm
http://www.javacertificate.net/passbyvalue.htm

No comments: