*
The moose likes Java in General and the fly likes IS it true that Java Uses only Call by value? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "IS it true that Java Uses only Call by value?" Watch "IS it true that Java Uses only Call by value?" New topic
Author

IS it true that Java Uses only Call by value?

vikram nalagampalli
Ranch Hand

Joined: Oct 08, 2001
Posts: 91
Hi all,
I recently attended an interview, where i was asked to differentiate between call by value and reference. Which i did it, later the interviewer asked if java uses call by value or call by reference. Which i was totally confused.
And so when i asked the interviewer what the correct answer was, he told me "java uses only Call by Value" and he did give me an explanation about why it is so. But infortunately i did not concentrate on his explanation and i am still wondering if some one could me a good explantion about what the interviewer would have meant by saying "Java uses only call by value...though it looks like Call by reference when passing objects"
Some one please validate the above statement.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

In JAVA, pass by value means basically that a copy of the variable is passed to a method.
Here is an example

The result is
1
0
The reason for this is that main passes a to compute(). compute() adds 1 to a making it 1. But that only changed it in that method. It only changed the copy. So when we print a after we called the method, it is 0.
You can pass by reference in JAVA. So the intervewer was not 100% accurate in his statement. Java does use pass by value all the time. But you can pass a method an object[] of some sort and the contents of that object[] will be passed by reference. Look at the following code:

The output here is:
1
1
Because we passed the int[] by value, but the value of int[0] was passed by reference. Cool huh. Hope that helps a little.


GenRocket - Experts at Building Test Data
Vijayakumar Arya
Ranch Hand

Joined: Jan 27, 2003
Posts: 76
Hi,
In general and in simple terms we can say that,
wherever you are passing variables of basic data types and literals, Java uses pass by value, i.e., a copy of the variable is used.
In case if you are passing variables of type object, then only the reference is copied and sent, you can say that it passing by reference.


Thanks,<p>Vijay<p>The Hand that gives, Gathers.
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
There was a heated discussion about this, what, a year ago, with among others myself and Roseanne Zhang involved. It ended up in a somewhat acrimoneous semantic mess. The final outcome was that, looking at the language as a whole, the most unambiguous point of view you can adopt is that Java only ever passes by value.
Let me clarify.Do you know these optical-illusion pictures where your eyes cannot make up their minds whether they see a shape sticking out of the background or a depression in the background? Similarly, there are two different ways you can look at this code.
  • "obj" is an object reference. It is passed into the method by value.
  • "obj" is the object. It is passed into the method by reference.
  • When you look at this little bit of code in isolation, both points of view are equally valid. There is no point in choosing between them; you can simply pick the picture you're most comfortable with.
    Things change when you start to look at these two pictures in the context of the rest of the language. Consider the assignmentLet's go through the two viewpoints again.
  • "obj" is an object reference. Its value is assigned to obj2; after the assignment, both obj and obj2 refer to the same object.
  • "obj" is the object. The "=" operator isn't assignment as you might expect, but an aliasing operator that turns "obj2" into an alias for "obj".
  • You see that the second picture, where "obj" is seen as "the object", requires you to adopt a very convoluted interpretation of the assignment operator. Arguably, convoluted enough that the viewpoint breaks down and becomes unmaintainable.
    As a consequence, the most unambiguous interpretation for "obj" is that of an object reference. Once you adopt this point of view, it follows that Java passes these object references by value -- indeed, that Java passes each and every method argument by value. In this sense, the interviewer was right.
    Hope this helps
    - Peter
    [ February 04, 2003: Message edited by: Peter den Haan ]
    vikram nalagampalli
    Ranch Hand

    Joined: Oct 08, 2001
    Posts: 91
    Thank a lot guys. Though it was bit confusing intitially, i was able to get to the point.
    Regards
    Vikram
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: IS it true that Java Uses only Call by value?