ws7ghq
Last Updated: November 21, 2017
·
6.733K
· orhanobut
62009 433414993266 4621115 n

java is always "pass by value"

Remember that when you pass a parameter in java, java is always "pass-by-value".

That means you can't change the reference of the parameter inside a method but its content can be changed.

Person person = new Person("Orhan");

System.out.println(person.name);     // Orhan
System.out.println(person);    // let's assume the ref address is 56

changeName(person);

static void changeName(Person p) {
  p.setName("The amazing Orhan");
  p = new Person("Not amazing Orhan");
}

System.out.println(person.name);    // The amazing Orhan
System.out.println(person);    //  56, ref address won't be affected
Say Thanks
Respond

13 Responses
Add your response

8547
245fe21

That is NOT a "pass by value" if you can actually change that value.
Pass by value in java is only for primitive variables (int, char...).
Every object in java is PASSED BY REFERENCE.

What you say about "changing the reference" can't be done in java, as you would need two levels of indirection (ie: passing a pointer by reference in C for example).

over 1 year ago ·
8608
62009 433414993266 4621115 n

@jsantix, what you say is a very wrong statement but some developers say that primitives are pass by value and objects are pass by reference. It's wrong.

Everything is pass by value in java.

A quote from the oracle about java.

Reference data type parameters, such as objects, are also passed into methods by value. This means that when the method returns, the passed-in reference still references the same object as before. However, the values of the object's fields can be changed in the method, if they have the proper access level.

http://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html

over 1 year ago ·
8789
245fe21

"REFERENCE data type parameters, such as objects, are also passed into methods by value."
Java passes objects' REFERENCES (aka: a memory pointers, 4-8 bytes, not the whole object) by value. That is actually a pass-by-reference.

That is because in fact, every object in Java is internally an object. If you don't trust me, try this:

ArrayList<Integer> a = new ArrayList<Integer>();
ArrayList<Integer> b = a;
a.add(42);
System.out.println(b.get(0)); //prints 42

As you can see, b = a just copied the reference of the object pointed by a into b, so both variables point to the same object. And exactly the same happens when you pass an object (actually, a variable pointing to an object) to a method. The method parameter is another variable pointing to the object whose reference you passed "by value".

Cheers!

over 1 year ago ·
8790
62009 433414993266 4621115 n

Your example doesn't prove the meaning of "pass-by-reference". If java had "pass-by-reference", you would be able to change the reference itself but you can't. That's why java says "everything in java is "pass-by-value". When you pass an object to method, of course reference of this object goes into the method, but the main thing is "You can not change reference inside the method". That's the important point.

Person p;
setName(p);

void setName(q){
   q = new Person("test");
}

System.out.println(q); // null

If java had pass by reference, output wouldn't be "null". That's how java defines the "pass-by-reference".

Check the link above.
http://www.yoda.arachsys.com/java/passing.html

over 1 year ago ·
8794
245fe21

Ok so "pass-by-reference" in Java is a totally different concept than "pass-by-reference" in C (and PHP, and many other languages), and objects "passed-by-value" behaves like a "pass-by-reference" in C (and PHP, and many other languages).

Call it whatever you like, but java does not copy whole objects when you pass them as a parameter to a function.
So if you a want to fully copy an object, you need to .clone() it.

Cheers.

over 1 year ago ·
8795
62009 433414993266 4621115 n

Yes, but I didn't say java copies the object, I already mentioned that it sends the reference of the object and it's obvious, otherwise you wouldn't be able to change the content of the object in the method.

over 1 year ago ·
9191
Default profile 2 normal

Java is always pass-by-value. In the case of object types, the value being passed is the reference (i.e. the pointer). In C/C++, "pass-by-reference" implies passing a pointer to a pointer. However, since Java doesn't support pointer types, this isn't possible; hence, no pass-by-reference in Java.

over 1 year ago ·
9198

jsantix is correct, nr4bt is wrong. Just because a language employs automatic dereferencing doesn't mean that a language always uses pass-by-value. If you had pass-by-value, a copy of the entire object would be made every time you made a function call, and whatever you did to the copy wouldn't affect the original. When you type 'p.setName("The amazing Orhan");', java automatically dereferences p and calls the objects function. When you type 'p = new Person("Not amazing Orhan");', what is actually happening is that you're overwritting the reference that p contains, with a reference to a new object.

By the logic that you, nr4bt, are using, pass-by-reference doesn't exist, for ANY language. Even C nor C++ would have pass-by-reference with this line of thinking.

You are simply wrong, nr4bt.

over 1 year ago ·
9199
C16f0f30170bb7df43273c6f678fc9bc

Ignoring the fact you can always caused a mutable object to alter its state Java indeed passes by value for the simple fact that in no case can you cause the caller's variable to point to an entirely new entity.

Private string someobj = new Object();

Somemethod(someobj);

The value of someobj cannot be switched out from under the caller by anything Somemethod does to its input parameter. If someobj is immutable then the caller knows that will be identical before and after the method call.
In a pass-by-reference situation it is possible for the caller's someobj to be different before and after the method call.

Thus indeed Java passes all parameters by value from the perspective of the caller. You can debate semantics with what exactly is being passed but in the end only this really matters - the behavior the caller sees and can control (i.e., by recognizing that mutable objects are at-risk but that immutable ones are not).

over 1 year ago ·
9204
Db46eae183b58be5ed88f619f7f74927

Calling a method with an object is passing the object's reference by value to the method. The value being passed is the reference to the object. A "reference to an object" is a primitive in Java just like an int or float. This is why, when you define the method parameters, they are variable declarations that receive their own copies of the values passed, i.e. pass by value. Pass by reference means the callee can change to what the caller's parameter points.

e.g. call by reference:
given:

void c(Object a) { a = null; }
b = new Object();
c(b); // Within c, a _is_ literally the same variable as b.
// Now b is null

But because Java isn't call by reference, b doesn't become null, a is not the same variable as b, it's only an alias to the same object to which b refers. That is, the value passed to c is the reference to the object to which b refers. a's value is a reference. And just because a is a reference value that's passed in, doesn't mean that the technique employed is pass by reference.

Look at it as if the value is a primitive:

void c(int a) { a = 0; }
b = 1;
c(b);

It's clear, right. a is a copy of b's value. It's exactly the same when passing an object reference.

But when variables in Java are objects their primitive data type is a pointer. The pointer is the value of the variable, not what it points to. You don't pass objects to methods. You pass references to those objects and that reference is the value passed.

over 1 year ago ·
9225

Actually, what you're describing is called call-by-sharing or pass-by-sharing, which is a variation of pass-by-reference. Pass-by-reference itself doesn't say anything about how a language handles the reference to the object, only that a copy of the object is not made and that only a reference is passed to the function through some means. In Java, references are passed to functions, and those functions can affect the state of the objects that the references are pointing to. The references can be overwritten, but since Java implements its object references through single-pointers, the original object the reference was pointing to isn't affected. This is in contrast to C++, where references is implemented through double-pointers, and the language allows the original object to be overwritten. Both however, employs pass-by-reference.

over 1 year ago ·
9734
Regent twitter

The confusion here, as others have said, is the definition of "reference".

In Java, you always pass a copy of the bits of the value of the reference. As far as I can tell, no one here disputes that. For primitives, this is the value of the reference. For Objects, this is the address of the object.

In both cases the whole value of the reference is copied.

So, the disagreement is whether or not this value is a value only or is actually a reference.

Here's my attempt to reconcile the disconnect: in the case of an Object, the value of the reference is itself a reference to the object (but it is only a reference in the sense that it's an address of the object). So, yes, you're passing a "reference" but that reference is an address and you've copied the whole value of that address and passed it.

This is, by definition, pass-by-value.

And that is why you can manipulate the object at the address that is passed in, but you cannot re-assign the reference that was passed in--because it wasn't the reference that was actually passed in, it was the address of the object.

See this StackOverflow thread.

over 1 year ago ·
28954

Java is always pass-by-value, not reference. Java does manipulate objects by reference, and all object variables are references. So Objects, Arrays, Primitive Types etc. – these are all passed by value in Java.
More....
http://net-informations.com/java/cjava/default.htm

Jomy

7 months ago ·