-1

Possible Duplicate:
Is Java pass by reference?

After reading the discussion on this topic, can one conclude that when Java passes a variable, say A (excluding primitive type), it means it passes the object, which the variable (A) is referencing. So any changes made on that object reflects in variable (A). So at last does it work as pass by reference in general term?

Community
  • 1
  • 1
Abhishek Bhandari
  • 613
  • 1
  • 5
  • 16
  • 2
    The reference of A is passed by value. The change you make to A in the method is local to that method only. A great write up on this topic can be found [here](http://javadude.com/articles/passbyvalue.htm) – CoolBeans Dec 15 '11 at 05:54
  • This thing is like everywhere. – Bhesh Gurung Dec 15 '11 at 06:01

5 Answers5

1

See Is Java "pass-by-reference" or "pass-by-value"?

Java is always calls-by-value. In the case of when objects are passed it passes the "value of the reference" which gives Java the semantics of call-by-object-sharing (it does this through call-by-value-of-the-reference though!).

I like to say: When an object is passed it is not copied. Then, since it is the same object on the inside of the method -- the variable is just a different "name" for it -- if you change the object (not the variable!) inside the method, you change that object outside -- everywhere, really -- as well. (It is the same object, ater all :-)

Please note that variables are never passed. They are evaluated as expression and the values that they evaluate to are passed.

Some languages like C++ support call-by-reference. This is different than either call-by-value or call-by-object-sharing because these functions are called with (a generally restricted set of) "lvalues" as arguments and reassignment to the parameters in the function will affect the "lvalues" on the outside. "lvalues" are normally variables and support for this kind of calling convention differs by language (many do not support it!).

Happy coding.

Community
  • 1
  • 1
1

No, that's not correct.

Everything is passed by value in JAVA - no exceptions!

However it's crucial to understand that what you pass is not the object, but a reference to it. This reference is passed by value (so you can't change it in the method). You indeed can alter the objects data through this reference, but as I said - you can't change the reference to refer to a different object (or null for that matter).

Petar Ivanov
  • 91,536
  • 11
  • 82
  • 95
  • Why can't we just accept call-by-object-sharing (for reference types) and get along? :p~ –  Dec 15 '11 at 06:02
1

Java passes parameters to methods using pass by value semantics. That is, a copy of the value of each of the specified arguments to the method call is passed to the method as its parameters.

Note very clearly that a lot of people confuse the "reference" terminology with respect to Java's references to objects and pass by reference calling semantics. The fact is, in dealing with Java's references to objects, a copy of the reference's value is what is passed to the method -- it's passed by value. This is, for example, why you cannot mutate the (original, caller's) reference to the object from inside the called method.

"pass by reference" means if you pass a variable into a method, its value can be modified. This is possible in many languages, like Pascal, Ada, and C++, but not in many other languages like C and Java.

here is a good discussion about it . http://www.jguru.com/faq/view.jsp?EID=430996

Hemant Metalia
  • 29,730
  • 18
  • 72
  • 91
0

Java does not pass by reference. It also does not pass objects. It passes object references by value. Inside a method, you can make changes to the object that was passed, but you cannot change the reference itself in the calling code.

void foo(Object obj) {
    foo = new Object();
}

Object obj = new Object();
Object obj2 = obj;
foo(obj2);
System.out.println("obj2 passed by " + (obj == obj2 ? "value" : "reference"));

This code will print "pass by value".

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
0

I would conclude more along the lines of:

Java passes all parameters, primitive and non-primitive, by value. In the case of non-primitive types, what Java actually passes is a pointer (or "reference") to the object instance, by value. This means that the caller's copy of the reference itself (i.e. the memory location that the pointer actually points to) cannot be changed. But at the same time, any state internal to the referenced object instance that the callee modifies will be modified in the caller's "copy" of the object as well, because there is in fact only a single instance of the object that both the caller and callee share.

aroth
  • 54,026
  • 20
  • 135
  • 176