This week's giveaway is in the Spring forum.
We're giving away four copies of Microservices Testing (Live Project) and have Chris Love & Andres Sacco on-line!
See this thread for details.
Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • 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
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic