java pass-by-XXXX doubt.

Hi All,please view the article:http://www.javaranch.com/campfire/StoryPassBy.jspit clearly says java is pass-by-valueI had an idea java is pass-by-reference.Please clarify if java language is pass-by-value or pass-by-reference?Rgds
[287 byte] By [ayusman_dikshita] at [2007-10-3 4:53:11]
# 1
JEEPERS!
warnerjaa at 2007-7-14 22:58:02 > top of Java-index,Java Essentials,Java Programming...
# 2
It's pass by vaule ONLY
Norweeda at 2007-7-14 22:58:02 > top of Java-index,Java Essentials,Java Programming...
# 3
@OP. Why doubt the article?
kajbja at 2007-7-14 22:58:02 > top of Java-index,Java Essentials,Java Programming...
# 4

> Hi All,

> please view the article:

>

> http://www.javaranch.com/campfire/StoryPassBy.jsp

>

> it clearly says java is pass-by-value

>

> I had an idea java is pass-by-reference.

> Please clarify if java language is pass-by-value or

> pass-by-reference?

>

> Rgds

http://www.google.com/search?sourceid=navclient-ff&ie=UTF-8&rlz=1B2GGGL_enUS176&q=java+pass+by+value

kilyasa at 2007-7-14 22:58:02 > top of Java-index,Java Essentials,Java Programming...
# 5

Why did you start another thread? You should have just stuck to your old thread. As that article and all the other links provided to you in your other thread will tell you, Java passes references by value. Those various links describe this thoroughly. If, after reading them, you're still confused, provide details. Just saying, "I think Java passes by reference," is like saying, "I didn't bother to read any of the articles. Please repeat what they say."

jverda at 2007-7-14 22:58:02 > top of Java-index,Java Essentials,Java Programming...
# 6
Java passes Reference Types by Value.This is different to pass by Reference.It is pass by value.The article is correct.
mlka at 2007-7-14 22:58:02 > top of Java-index,Java Essentials,Java Programming...
# 7

All parameters to methods are passed "by value." In other words, values of parameter variables in a method are copies of the values the invoker specified as arguments. If you pass a double to a method, its parameter is a copy of whatever value was being passed as an argument, and the method can change its parameter's value without affecting values in the code that invoked the method. For example:

class PassByValue {

public static void main(String[] args) {

double one = 1.0;

System.out.println("before: one = " + one);

halveIt(one);

System.out.println("after: one = " + one);

}

public static void halveIt(double arg) {

arg /= 2.0;// divide arg by two

System.out.println("halved: arg = " + arg);

}

}

The following output illustrates that the value of arg inside halveIt is divided by two without affecting the value of the variable one in main:before: one = 1.0

halved: arg = 0.5

after: one = 1.0

You should note that when the parameter is an object reference, the object reference -- not the object itself -- is what is passed "by value." Thus, you can change which object a parameter refers to inside the method without affecting the reference that was passed. But if you change any fields of the object or invoke methods that change the object's state, the object is changed for every part of the program that holds a reference to it. Here is an example to show the distinction:

class PassRef {

public static void main(String[] args) {

Body sirius = new Body("Sirius", null);

System.out.println("before: " + sirius);

commonName(sirius);

System.out.println("after: " + sirius);

}

public static void commonName(Body bodyRef) {

bodyRef.name = "Dog Star";

bodyRef = null;

}

}

This program produces the following output: before: 0 (Sirius)

after: 0 (Dog Star)

Notice that the contents of the object have been modified with a name change, while the variable sirius still refers to the Body object even though the method commonName changed the value of its bodyRef parameter variable to null. This requires some explanation.

The following diagram shows the state of the variables just after main invokes commonName:

__

main()| |

sirius->| idNum: 0|

| name --+>"Sirius"

commonName()-->| orbits: null |

bodyRef|__|

At this point, the two variables sirius (in main) and bodyRef (in commonName) both refer to the same underlying object. When commonName changes the field bodyRef.name, the name is changed in the underlying object that the two variables share. When commonName changes the value of bodyRef to null, only the value of the bodyRef variable is changed; the value of sirius remains unchanged because the parameter bodyRef is a pass-by-value copy of sirius. Inside the method commonName, all you are changing is the value in the parameter variable bodyRef, just as all you changed in halveIt was the value in the parameter variable arg. If changing bodyRef affected the value of sirius in main, the "after" line would say "null". However, the variable bodyRef in commonName and the variable sirius in main both refer to the same underlying object, so the change made inside commonName is visible through the reference sirius.

Some people will say incorrectly that objects are passed "by reference." In programming language design, the term pass by reference properly means that when an argument is passed to a function, the invoked function gets a reference to the original value, not a copy of its value. If the function modifies its parameter, the value in the calling code will be changed because the argument and parameter use the same slot in memory. If the Java programming language actually had pass-by-reference parameters, there would be a way to declare halveIt so that the preceding code would modify the value of one, or so that commonName could change the variable sirius to null. This is not possible. The Java programming language does not pass objects by reference; it passes object references by value. Because two copies of the same reference refer to the same actual object, changes made through one reference variable are visible through the other. There is exactly one parameter passing mode -- pass by value -- and that helps keep things simple.

-- Arnold, K., Gosling J., Holmes D. (2006). The Java?Programming Language Fourth Edition. Boston: Addison-Wesley.

yawmarka at 2007-7-14 22:58:02 > top of Java-index,Java Essentials,Java Programming...
# 8

Simply great peace of article.

Thanks for that.

I was going through "Programming Languages design and implementation" , By Pratt and Marvin, are "Call-by-value" and "Pass-by-value" are one and the same thing?

If I got it right, then in java we pass refernces (when it's not a primitive data type), but the method used for sub-program call is call-by-value.

Please correct me if I am wrong.

Regards

Ayusman

ayusman_dikshita at 2007-7-14 22:58:02 > top of Java-index,Java Essentials,Java Programming...
# 9

> If I got it right, then in java we pass refernces

> (when it's not a primitive data type), but the method

> used for sub-program call is call-by-value.

I don't know what you mean by that.

Did you read post #8 by yawmark? It's a really good post.

Kaj

kajbja at 2007-7-14 22:58:02 > top of Java-index,Java Essentials,Java Programming...
# 10

> If I got it right, then in java we pass refernces

> (when it's not a primitive data type)

In Java, when we invoke a method, all parameters are passed by value.

Primitives are passed by value.

References are passed by value.

Null is passed by value.

This means that the value of the parameter is copied and passed.

Note that object are not passed at all. Rather, references are passed by vvalue--the reference's value (which is NOT an object) is copied and passed.

>, but the method

> used for sub-program call is call-by-value.

This doesn't really make any sense--it doesn't use standard terminology.

It might be that you're trying to repeat what another poster said--that when we invoke an object's method, we do so through a reference to that object. If that's what your trying to say, then it's correct, but 1) It has nothing to do with parameter passing and 2) we don't call that "call by " something. "Call by" is not used to mean that.

jverda at 2007-7-14 22:58:02 > top of Java-index,Java Essentials,Java Programming...