• Post Reply Bookmark Topic Watch Topic
  • New Topic

java is call by value or call by ref.  RSS feed

 
raj rajeevg
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is java is call by value or call by reference. Is there any case where we can say java is call by reference. Please explain.
 
Sidd Kulk
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java is always call by value. Java passes the copy of bits present in the container for the variable. Thus, in case of primitives, it passes a copy of actual value of the primitive, because the bit holder for primitive contains its actual value. While in case of object reference variables, the bit pattern represents a way to point(or in java terms, refer) to the object on heap. Remember, a reference variable never contains the actual object. All object reference variable in a single JVM have same bit depth.


Sid
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
 
rohit leeta
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess there is an error in the last sentence of that FAQ item.

It is:

"In short in java, object references are passed by value and primitive types are passed by value."

Shouldn't it be:

"In short in java, object references are passed by reference and primitive types are passed by value."
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope, it's correct. References are passed by value. Read it again
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the original sentence is correct. Consider:

If Java used pass by reference, then setting s = "Bye" in method() would have the effect of changing the s in main(). It doesn't, because Java does not pass by reference, even when it's passing references.
 
Sidd Kulk
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Your code explains the same fact that java uses pass by value. When you pass an object reference variable, you pass a copy of the bits contained in that variable, which actually is a way to point to that object. And when you pass that reference to a method, the reference becomes local to the method and its scope is limited to method. So when you reassign it, the reference changes, that is, Object to which the variable refers, changes.

In java, String objects are given special treatement. They don't behave like other object because of their immutability. That is, String objects, once formed are immutable. In your example, this property of Strings doesn't let you alter the object.
You can try it using a StrinBuffer.

Try something like this:


You will notice that, when in the called method, we change the reference, then there is no change in the value of original object. But as we pass the reference, we can always change the original object.
This, though, is not true in case of Strings.
Sid
[ May 15, 2007: Message edited by: Sidd Kulk ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!