1

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
  • you can not perform constant++ i.e. 5++. In your method you are return 0 ; – Rahul Jul 11 '14 at 12:24
  • How different is obj.get()+1 from obj.get()++ ? – Kartik_Koro Jul 11 '14 at 12:25
  • What would you expect it to do? Translate into a call to `setInteger(getInteger() + 1)`? Don't forget that the Java *language* doesn't know anything about getters and setters... – Jon Skeet Jul 11 '14 at 12:27
  • I don't think this question is useful, because it can't be asked by someone who actually understands what `++` means. If you ask yourself *"why is there a `++`, anyway, when `+1` is just as easy to write?"*, and can answer correctly, then you already know the problem with the code in this question. (But then, it's a duplicate of something that got a lot of upvotes, so...) – Karl Knechtel Jun 29 '23 at 03:30
  • Does this answer your question? [Why doesn't the post increment operator work on a method that returns an int?](https://stackoverflow.com/questions/15291861/why-doesnt-the-post-increment-operator-work-on-a-method-that-returns-an-int) – Karl Knechtel Jun 29 '23 at 03:34

5 Answers5

10

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.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
2

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nobalG
  • 4,544
  • 3
  • 34
  • 72
1

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; ??
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kartik_Koro
  • 1,277
  • 1
  • 11
  • 23
0

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.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
0

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();
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130