Reputation: 37034
I noticed a surprising behaviour.
I can write so:
Integer integer = 0;
integer++;
But I cannot write so:
getInteger()++;
Here's my getInteger()
method:
public static Integer getInteger(){
return 0;
}
For me, two snippets look the same.
But the second piece of code returns an error:
unexpected type
getInteger()++;
^
required: variable
found: value
Why is the second construction forbidden?
Often, I feel forced to write "ugly" code like this:
obj.set(obj.get()+1);
It looks redundant.
Upvotes: 1
Views: 893
Reputation: 339816
You are not returning an int
; you're returning an Integer
object. The Integer
class is immutable and has no ++
operator of its own.
Also, the ++
operator requires that its operand be an lvalue
, i.e. an assignable variable as opposed to a value
. The result of getInteger()
is the latter.
In the case of your first test:
Integer integer = 0; integer++;
Java's autoboxing allows the use of the ++
operator, but only because Java automatically converts the Integer
into an int
and then back again, and because the variable integer
is an lvalue
. It's more or less equivalent to:
int tmp_int = integer.intValue();
int result = tmp_int++;
integer = Integer.getInteger(tmp_int);
(void)result;
result
- the effective "value" of the expression isn't integer
because you've used the "post increment" version of ++
, so the result is the value that integer
had before it was incremented.
Upvotes: 0
Reputation: 1287
As it says, it requires a variable for ++
operator. ++
increments a value that is stored somewhere, so the incremented value is now stored in that location. Your function just returns a value, which is not a variable.
It's as simple as Rahul mentioned in the comment.
//This works
x=5;
x++; //Now x=6 since x++ is equivalent to x=x+1;
// This doesn't work
5++; // This doesn't even make sense, trying to do 5=5+1??
// Similarly, getNumber() is just a value that is returned, it is a value, not a variable
// So it is like the second case
// This doesn't work
getNumber()++; //Trying to do getNumber()=getNumber()+1; ??
Upvotes: 1
Reputation: 4620
8++
will not make sense, while
int x=8;
x++;
does make sense, because x++, is working like x+=1
, i.e., x=x+1
and 8=8+1
is something not acceptable.
Upvotes: 2
Reputation: 533492
Why is the second construction forbidden?
Integer
is immutable, so you couldn't change it anyway. int
is mutable but since it only returns a copy on the value, this won't work either.
E.g.,
public static Integer getInteger(){
return 0;
}
// what would this do
getInteger()++;
// now the method has been changed to
public static Integer getInteger(){
return 0+1;
}
// so now
System.out.println(getInteger()); // prints 1 ??
obj.set(obj.get()+1);
This breaks encapsulation as well. I would suggest a method like this:
AtomicInteger i = new AtomicInteger(0);
i.incrementAndGet();
Or you could have your own:
i.incr();
Upvotes: 0
Reputation: 363547
x++
is not the same as x + 1
. It's short for x += 1
, i.e. x = x + 1
; increment, then assign. So getInteger()++
means getInteger() = getInteger() + 1
, which makes no sense. You're adding one to a value, not incrementing a variable.
Apart from that, ++
works on int
but not Integer
, but even if you were returning an int
you couldn't increment it with ++
:
class Test {
static public void main() {
getint()++;
}
static int getint() {
int a = 0;
return a;
}
}
gives
Test.java:3: error: unexpected type
getint()++;
^
required: variable
found: value
(In C++ you could make this work with references, but Java doesn't have those.)
Upvotes: 10