Unary operators

Figure 171. Increment operator ++ Slide presentation Create comment in forum

Increment variable by 1:

int a = 4;

a = a + 1;

System.out.println("Value:" + a);
int a = 4;

a++;

System.out.println("Value:" + a);
Value:5

Figure 172. Prefix and postfix notation Slide presentation Create comment in forum
pre-increment post-increment
int a = 4;
int b = ++a;
System.out.println(b);
int a = 4;
int b = a++;
System.out.println(b);
Output:
5
Output:
4

exercise No. 54

Three ways expressing the same Create comment in forum

Q:

Write the statement a = a - 1; in two alternate ways.

Tip

Use different operators.

A:

  • a -= 1;

  • a--;

Figure 173. Operator examples Slide presentation Create comment in forum
//Integer
i--;             i = i - 1;  // Decrement by one
i += k;          i = i + k;  // Raise by k's value
i %= 3;          i = i % 3;  // Modulus of 3

// boolean
                    b = !b;  // Switching true <--> false

exercise No. 55

Guessing results Create comment in forum

Q:

Consider the following code segment:

int a = 3;
a++;          // Incrementing a by 1 --> a==4

int b = a;    // TODO

b--;          // TODO
--b;          // TODO

int c = b;    // TODO

b = ++a;      // TODO
int e = a++;  // TODO

a *= b;       // TODO

System.out.println("a=" + a);
System.out.println("b=" + b);
System.out.println("c=" + c);
System.out.println("e=" + e);

Guess the expected result without executing the above code. Replace the TODO sections by explanations among with expected variable values.

Then copy/paste the above code into a main() method body and control your findings (possibly wailing about your success rate).

Tip

Both x++ (postfix notation) and ++x (infix notation) are expressions themselves which might even be rewritten as (x++) and (++x) for the sake of clarity. The difference is not about operator precedence rules but simply about the values of these expressions when being assigned to other variables.

A:

As inferred by the hint the biggest problem is about understanding postfix and infix notation of the operators ++ and --. A corresponding expression evaluates to:

  • a = x++ yields a =x: The value of x before being incremented.

  • a = ++x yields a = x + 1: The value of x after being incremented.

The remaining task is just obeying the due diligence rule set:

int a = 3;
a++;          //Incrementing a by 1 --> a==4

int b = a;    // Assigning value of a --> b==4

b--;          // Decrementing b by 1 --> b==3
--b;          // Decrementing b by 1 --> b==2

int c = b;    // c == 2;

b = ++a;      // Incrementing a by 1 --> a==5, then assigning to b --> b == 5
int e = a++;  // Assigning a to e --> e==5, then incrementing a --> a==6

a *= b;       // Multiplying a with b and assigning the result to a --> a==30

System.out.println("a=" + a);
System.out.println("b=" + b);
System.out.println("c=" + c);
System.out.println("e=" + e);

exercise No. 56

Cleaning up the mess Create comment in forum

Q:

Some developers follow the rule It was hard to write, it should be hard to understand as well. One such proponent codes:

int a = 3, b = 6, c = 2;

a += (a = 2) + ++a - ++b % c--;

System.out.println("a = " + a + ", b = " + b + ", c = " + c);

Execution results in:

a = 7, b = 7, c = 1

Decompose this cryptic assignment into a series of multiple elementary ones like e.g. a++ by possibly introducing one or more helper variables. Thus your code shall become longer but better to understand.

Tip

Read about evaluation of expressions and operator precedence.

A:

We start by modifying the line in question:

a = a + (a = 2) + ++a - ++b % c--;

The expression on the right hand side will be decomposed by the Java compiler into a sum of four subexpressions to be evaluated from left to right:

  1. a

  2. a=2

  3. ++a

  4. ++b % c--

    This one deserves further attention: The % operator will act on b's value after incrementing (infix notation) and c's value before decrementing (postfix notation).

We introduce a helper variable sum for decomposing our code into the above described four subexpressions:

int a = 3, b = 6, c = 2;

int sum = a;  // First subexpression
a = 2;
sum += a;     // Second subexpression
a++;
sum += a;     // Third subexpression
b++;
sum -= b % c; // Fourth subexpression
c--;

a = sum;      // Assigning the result

System.out.println("a = " + a + ", b = " + b + ", c = " + c);