• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

java is call by value or call by ref.

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the explanation in the JavaRanch Beginners FAQ.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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."
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nope, it's correct. References are passed by value. Read it again
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Good night. Drive safely. Here's a tiny ad for the road:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic