The result for all of those is n=0. Integer n is an object and therefore passed by reference, so why isn't the increment reflected back in the caller method (main)? I expected output to be n=0 n=1 n=2 etc...

UPDATE:
Notice I updated the code example above. If I'm understanding correctly, Jon Skeet answered the question of why myInt would increment and why n does not. It is because n is getting a new reference assigned in the Increment method. But myInt does NOT get assigned a new reference since it's calling a member function.

9 Answers
9

No, objects aren't passed by reference. References are passed by value - there's a big difference. Integer is an immutable type, therefore you can't change the value within the method.

Your n++; statement is effectively

n = Integer.valueOf(n.intValue() + 1);

So, that assigns a different value to the variable n in Increment - but as Java only has pass-by-value, that doesn't affect the value of n in the calling method.

EDIT: To answer your update: that's right. Presumably your "MyIntegerObj" type is mutable, and changes its internal state when you call plusplus(). Oh, and don't bother looking around for how to implement an operator - Java doesn't support user-defined operators.

What? Yes, I mean it works, but it's not really a good idea. It will do things like synchronize the buffers of your processors, since it's not an atomic operation otherwise. Really, you shouldn't use an atomic version of a data type unless you have a need for that atomicity...
–
JasperJan 9 at 8:37

@Jasper, I can't argue with that. It's just another tool in my toolbox. I choose depending on the task at hand. And unless the profiler points out a problem at that particular point, it remains an option.
–
DerMikeJan 13 at 12:22

As Jon Skeet mentioned, all methods in Java are pass-by-value, not pass-by-reference. Since reference types are passed by value, what you have inside the function body is a copy of the reference - this copy and the original reference both point to the same value.

However, if you reassign the copy inside the function body, it will have no effect on the rest of your program as that copy will go out of scope when the function exits.

But consider that you don't really need pass-by-reference to do what you want to do. For instance, you don't need a method to increment an Integer - you can just increment it at the point in your code where it needs to be incremented.

For more complex types, like setting some property on an Object, the Object you are working with is likely mutable; in that case a copy of the reference works just fine, since the automatic dereference will get you to the original object whose setter you are calling.

Side note: In my humble opinion, writing n++ on an Integer object is extremely misleading. This relies on a feature of Java called "autoboxing" where it converts primitives to their corresponding boxing objects -- like int to Integer or boolean to Boolean -- automatically and without warning. Frankly I think it's a bad feature. Yes, sometimes it makes your life simpler by automatically doing a handy conversion for you, but it can also do conversions that you did not intend and do not realize are happening, with unintended side effects.

Anyway, there are two ways to accomplish what you are apparently trying to do:

One: Have the increment function return a new Integer with the updated value. Like: