Is there a way to pre-increment by more than 1 in Java?

In Java, you can increment by mail the integer i in more than this way:

j + i += 2.

I would like to do the same with pre-increment.

eg j + (2 += i) //This will not work

0
source share
4 answers

Not sure if there is any confusion in terminology, but += not a post or pre-increment statement! Java follows the C / C ++ definition for post-increment / pre-increment, and they are well defined in the standard as unary operators. +=is a shortcut for a binary operator. He appreciates this:

lvalue1 += 5 ;
// is really (almost)
lvalue1 = lvalue1 + 5;

The assembler for the command does not look like the binary version, but at the level using Java, you do not see this.

-/ - , :

i++ ; // is something like _temp = i; i = i + 1; return temp;
++i;  // is something like i = i + 1; return i;

, , - .

, -, . , () , . C, ++ Java.

+2

increment . , pre: 2:

int i = 0;
System.out.println(
    ((i+=2) == 0)
    ? "post: " + i : "pre: " + i);

, , , . .

+3

- :

int increment = 42;
int oldI = i;
i += increment;
result = myMethod(oldI);
// Rest of code follows.

?

+1

+=is a preliminary increment. If you want post increment to simply create a wrapper using the postInc method that will do this. If you really need it, it will be more readable than parentheses.

0
source

All Articles