tags:

views:

108

answers:

4

Hello
In C++ you can do the following:

int x = 5

cout << x;
// prints 5

int* px = &x;

(*px)++;
cout <<x;
// prints 6

Is there an equivalent construct in Java

A: 

No... Java abstracts away the concept of pointers and memory locations. Pointers, and pointer dereferencing are not possible in Java.

Collin
+3  A: 

In order to do this, you have to create an object that contains an int. Then, you can pass around a reference to that object, and you can increment the int that's in that object. But there's no way that you can have another reference to the same int and increments in one will increment the other -- unless both cases are references to the same wrapper object.

Erick Robertson
+1, there's even a generic class that already does this: [Holder<T>](http://download.oracle.com/javase/6/docs/api/javax/xml/ws/Holder.html)
casablanca
That won't work in this particular case because `Integer` is immutable: you can pass a Holder<Integer> object around, but you still can't change the Integer it holds.
Cameron Skinner
+1  A: 

Not as such.

You can think of Java as passing everything by reference, so

Foo f1 = new Foo();
Foo f2 = f1;

means f2 and f1 point to the same object (unlike C++ where the assignment would make a copy). Equivalent C++ code would make f2 a Foo&.

Java will autobox ints to Integers, but Integers are immutable. So you need to either write your own MutableInteger, or use something like the java.util.AtomicInteger if you want to pass by reference and change values as a side effect (which I suspect is your underlying motive).

Sadly you can't override operators in Java so the syntax will be more verbose then equivalent C++ code.

Cameron Skinner
sadly? that's one of java's features :-)
seanizer
Heh. It would be nice sometimes. So would `const`, for that matter (and no, `final` just doesn't cut it).
Cameron Skinner
BTW Java passes everything by value, even references. There is no call by reference in Java.
seanizer
Any reason for the downvote? EDIT: I said "You can think of Java as passing everything by reference". Emphasis on "you can think". For a C++ programmer this is the best way to transition to Java-land.
Cameron Skinner
+2  A: 

The answer is "sort of". As long as you're dealing with primitives, just wrap the value in an array.

int[] a = {5};
int[] b = a;
b[0]++;
System.out.println(a[0]);

As others have pointed out, for more complex problems, your best bet is always to create an object with an 'increment' or 'set' method that modifies a stored value.

RD
Does Java not do auto-boxing of primitives.
Martin York
I apologize, I'm not entirely sure what you're getting at, but I'll try to respond. Java only auto-boxes primitives when converting to and from the auto-boxing wrapper Classes (ie Integer, Short, Long, etc...). Otherwise Java does indeed treat primitive types as primitives. http://leepoint.net/notes-java/data/basic_types/autoboxing.html shows how some code statements in Java are interpreted.
RD