Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Why is java "call by value" ?  RSS feed

 
pratapsiva sivakumar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi why is java strictly called "call by value" and not "call by reference" .Please Explain with an example?
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pratap,

Nice little article here.
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • 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.
 
James Ward
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • 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.
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Maybe this article gives some hint http://javadude.com/articles/passbyvalue.htm


This is a superb article. Good one.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16026
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!