Skip to main content
IBM 
ShopSupportDownloads
IBM HomeProductsConsultingIndustriesNewsAbout IBM
Java Language EssentialsDownload tutorial zip fileEmail this tutorial to a friend
Main menuSection menuGive feedback on this tutorialPrevious panelNext panel
Course Notes
  


Multifunction operators page 18 of 37


We've mentioned that Java(TM) is syntactically powerful, like C. the Java programming language supports several powerful, multifunction operators described in the following table:

Multifunction
Operator
Interpretation
++increment (by 1)
--decrement (by 1)
+=increment (by specified value)
-=decrement (by specified value)
*=multiply (by specified value)
/=divide (by specified value)
&=bitwise and (with specified value)
|=bitwise inclusive or (with specified value)
^=bitwise exclusive or (with specified value)
%=integer remainder (by specified value)

These operators (actually operator combinations) are multifunction operators in the sense that they combine multiple operations: expression evaluation followed by variable assignment. For example, x++ first evaluates x, increments the resulting value by 1, assigns the result back to x, and "produces" the initial value of x as the ultimate evaluation. In contrast, ++x first evaluates x, increments the resulting value by 1, assigns the result back to x, and produces the updated value of x as the ultimate evaluation.

Note that x++ and ++x are equivalent in standalone contexts where the only task is to increment a variable by one, that is, contexts where the ultimate evaluation is ignored:


int x = 4;
x++; // same effect as ++x
System.out.println("x = " + x);

This code produces the output:


x = 5

In the call to println(), the argument is a concatenation of the string "x = " and x after its conversion to a string. String operations, including the use of + for string concatenation, are discussed in the panel Strings.

In the following context, the placement of the increment operator is important:


int x = 4;
int y = x++;
int z = ++x;
System.out.println(
  "x = " + x + " y = " + y + " z = " + z);

This code produces the output:


x = 6 y = 4 z = 6

The following table includes examples and interpretations:

Multifunction
Operator
ExamplePedestrian
Equivalent
++x++, ++xx = x + 1
--x--, --xx = x - 1
+=x += yx = x + y
-=x -= yx = x - y
*=x *= yx = x * y
/=x /= yx = x / y
&=x &= yx = x & y
|=x |= yx = x | y
^=x ^= yx = x ^ y
%=x %= yx = x % y

Note that Java restricts bitwise operations with &, |, and ^ to integer values, which makes sense. Further information on binary operations is available in many introductory computer science texts.

Using the decrement operator we can rewrite the iterative operation in bark() somewhat more succinctly as follows:


  void bark(int times) {
    while (times > 0) {
      System.out.println(barkSound);
      times--;
    }
  }

Further code reduction is possible:


  void bark(int times) {
    while (times-- > 0)
      System.out.println(barkSound);
  }

In this case, we use the ultimate evaluation of times in the while construct's Boolean expression that controls iteration (loop continuation). In particular, times-- decrements the variable but "produces" the initial value of times for the expression evaluation preceding the greater-than comparison operation.


copyright 1996-2000 Magelang Institute dba jGuru


Main menuSection menuGive feedback on this tutorialPrevious panelNext panel
PrivacyLegalContact