Unary operators

Figure 125. Increment operator ++ Slide presentation
Increment variable by 1: Shorthand version:
int a = 4;

a = a + 1;

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

a++;

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

Figure 126. Different range behaviour! Slide presentation
Increment variable by 1: Shorthand version:
byte value = 127; // Max possible value

value = value + 1; // Error: Required type: byte
                   // Provided:int

System.out.println(value);
byte value = 127; // Max possible value

value++; // o.K., will cycle through range

System.out.println(value);
Does not compile
Value:-128

Figure 127. Cast required Slide presentation
Increment variable by 1: Shorthand version:
byte value = 127; // Max possible value

value = (byte)(value + 1); // cast required,
                           // possible overflow

System.out.println(value);
byte value = 127; // Max possible value

value++; // cycle through range,
         // no cast required. 

System.out.println(value);
Value:-128

Figure 128. Prefix and postfix notation Slide presentation
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. 53

Three ways expressing the same

Q:

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

Tip

Use different operators.

A:

  • a -= 1;

  • a--;

Figure 129. Operator examples Slide presentation
Shorthand Operation Explicit
//Integer operations
i--;    
i += k;
i %= 3;

// boolean
  - nothing appropriate -
    Decrement by one
    Raise by k's value
   assign modulus of 3



Switching true <--> false             
//Integer operations
i = i - 1;
i = i + k;
i = i % 3;

// boolean
b = !b;

exercise No. 54

Guessing results

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

Cleaning up the mess

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