+8

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

I always thought Java was pass-by-reference. However, I've seen a couple of blog posts that claim that it isn't. I don't think I understand the distinction they're making. What is the explanation?

11/7/2019 7:20:17 PM

₦ØØⱤ ₳Ⱡł

21 Answers

New Answer

+10

Java is pass-by-value.

+8

So isn't it really a mix then? Couldn't you by the same logic otherwise say that C++ is 'always by value', since for pointers and references a pointer 'value' will be copied?

+8

HonFu Well you can say that. Probably Java guys wanted to keep a cleaner image, so they said every thing is passed by value 😁 At the deepest technical level all languages are pass by value. But some want to make distinction.

+7

Isn't it a mix? (Primitives by value, objects by reference)

+7

HonFu No their Reference(address) is copied to the stack. All variables have address. For primitives the value is copied for compound objects their reference is copied by value.

+7

It's a bit strange though, isn't it? I mean, when you pass something by reference/pointer in C/C++, you normally have mainly two reasons: 1.) You don't want to waste resources copying large objects, 2.) You want to modify the thing you pass as an argument. Aren't passed objects in Java accomplishing exactly that? So why would you go out of your way to call something the opposite of what it is?

+7

HonFu 😁 Yes they do. I think It's more of a language propaganda. Something like look no pointers, no reference (C++ style) just pass the object and modify it if you want. But you have only two options either copy the whole object (which they could not afford as java is already memory heavy) or copy the reference and keep track internally.

+7

Thank you, that was exactly my suspicion. 😜

+7

Primitive types pass a "copy" of there value refrenece types pass there actual value which is the refrenece and unlike primitive types theres no copy.

+6

Java is pass by value. Here you cannot pass the address of a variable to a function so pass by reference is not possible.

+6

It's always pass by value

+5

Java is always Pass By Value

+5

According to the above link, Java manipulates objects by references but passes object references by value into methods.

+5

According to the below link, if you want to swap objects within a method in Java, you have to use wrapper classes. https://www-geeksforgeeks-org.cdn.ampproject.org/v/s/www.geeksforgeeks.org/swap-exchange-objects-java/amp/?amp_js_v=a2&amp_gsa=1&usqp=mq331AQCKAE%3D#aoh=15732464753083&amp_ct=1573246482202&referrer=https%3A%2F%2Fwww.google.com&amp_tf=From%20%251%24s&ampshare=https%3A%2F%2Fwww.geeksforgeeks.org%2Fswap-exchange-objects-java%2F

+5

Now I know another reason why Java is overly verbose.

+4

So objects of classes, no matter how big, are always completely copied to the parameter when you pass them as args?

+4

https://www-javaworld-com.cdn.ampproject.org/v/s/www.javaworld.com/article/2077424/learn-java-does-java-pass-by-reference-or-pass-by-value.amp.html?amp_js_v=a2&amp_gsa=1&usqp=mq331AQCKAE%3D#aoh=15732453938105&referrer=https%3A%2F%2Fwww.google.com&amp_tf=From%20%251%24s&ampshare=https%3A%2F%2Fwww.javaworld.com%2Fjavaworld%2Fjavaqa%2F2000-05%2F03-qa-0526-pass.html

+3

Not only Java , most of the programming language pass-by Value 🙂

+3

Java is always works on "pass by value" and it doesn't support the use of point to refer the value address Because the memory allocation and deallocation work automatically.Then the address can't be referred. But u can use object reference to call the value , not to refer it

+2

java is by value all the time