• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why is java "call by value" ?

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi why is java strictly called "call by value" and not "call by reference" .Please Explain with an example?
 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pratap,

Nice little article here.
 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

pratapsiva sivakumar wrote:Hi why is java strictly called "call by value" and not "call by reference" .Please Explain with an example?



Also Search First. You will surely get valuable replies as this question already been asked in the forum.
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It may appear that:
1. primitive data types are passed by value
2. objects are passed by reference (since when you modify the object the original object is what gets modified)

But, the second statement is not strictly true.

When you pass objects to methods, what you are actually passing is reference(pointer) to the object (and not the object itself).
In java this reference is passed 'by value', in other words the reference/pointer is copied and passed (just like a value).

So the method receives an object handle; by value.

Therefore, in java method parameters are passed 'by value' both primitives and objects.
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Balu Sadhasivam wrote:Pratap,

Nice little article here.



It explains what is Pass By Value but does not answer the original question of why should java use pass by value? Maybe this article gives some hint http://javadude.com/articles/passbyvalue.htm
 
James Ward
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maybe this article gives some hint http://javadude.com/articles/passbyvalue.htm



This is a superb article. Good one.
 
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

Jothi Shankar Kumar wrote:It explains what is Pass By Value but does not answer the original question of why should java use pass by value?


In Java, variables of non-primitive types are references to objects. This is different from C++, where such a variable is the object itself. There are no variables that directly represent objects in Java.

Java has inherited a lot of its ideas from C++, but the designers of the Java programming language wanted to leave the complicated stuff from C++ out. Passing by reference was probably one such thing, and since non-primitive type variable are references already, there was really no need to have both pass-by-value and pass-by-reference in Java - that would make the language more complicated with little benefit.

Note that in Java, references are passed by value - that's something different than passing by reference.
 
reply
    Bookmark Topic Watch Topic
  • New Topic