The Unary Operators


x

Lesson:
Main Points:
  1. Unary operators work on a single operand
  2. The increment and decrement operators: ++ --
  3. The unary plus and minus operators: + -
  4. The bitwise inversion operator: ~
  5. The boolean complement operator: !
  6. The cast: ()

Most operators take two operands. For instance, in multiplication the operator works on two values. Unary operators work on a single value at a time.

Increment and Decrement Operators:
The ++ and the -- simple increment or decrement a value. They are used like this:

  x++;
  y--;
  ++x;
  --y;
The ++ and the -- can be placed before or after the operand as you can see in the above example. This makes a difference in timing. Consider these two examples:
  x = y++;
  x = ++y;
In the first case x is assigned the value held by y and then y is incremented. In the second case y is incremented and then x is assigned that value.

Unary + and - Operators:
Here are a few examples for you to ponder.

x = -5;
y = +4;
z = -(x-7);
The effect of the - in the first example is to signify a negative value. The + in the second example is redundant or unnecessary and only serves to emphasize that the value 4 is positive. In the third example we see a unary - operator (in front of the parentheses) and the - arithmetic operator between the x and the 7. The first - negates whatever the result of the operation inside the parentheses. So, -(-7) equals positive 7 and -(7) equals negative 7.

The Bitwise Inversion Operator:
To perform bitwise inversion on itegral types you used the ~ (tilde). This changes a value at the binary level so that if you have a value which is represented as 00110011, it will become 11001100 after applying the bitwise inversion operator. In general all 1 bits become 0s and all 0 bits become 1s. Here's how it is used:

x = ~x;
x = ~y;
x = ~5; // stores bitwise inversion of binary representation of five
The Boolean Complement Operator:
The ! operator inverts the value of a boolean expression. So !true gives false and !false gives true. Here's an example:
boolean flag = false;
flag = !flag; // changes value of flag to true
The Cast Operator:
This allows the programmer to override the assigned type. For instance, if you want to treat a float value as if it were an integer you would do the following:
float f = 5.55;
int g = (int)f; 
int circumference = (int)(Math.PI * diameter);
There are many practical situations were this is useful, but the programmer must be aware of the possiblity of value overflow. BTW you can assign an integer value to a float because there is no possibility of loss of precision.

EXAMPLE CODE:

import java.applet.*; import java.awt.*; public class j201 extends Applet{ int size; double circ, inc; boolean done; Dimension d; Image i; public void init(){ setBackground(Color.white); d = getSize(); i = createImage(d.width,d.height); makePic(); } public void makePic(){ Graphics x = i.getGraphics(); x.setColor(Color.blue); size=0; circ = inc = 2.02; do{ size += (int)circ; //shows use of cast x.drawOval(d.width/2-size/2, d.height/2-size/2, size, size); circ+=1.01; if(size > d.width) done = true; }while(!done); //shows use of Boolean complement x.dispose(); } public void paint(Graphics g){ g.drawImage(i,0,0,this); g.setColor(Color.red); g.drawRect(0,0,d.width-1, d.height-1); } }


x

Assignment:
Write a short applet which displays the difference between the effect of the unary minus and the bitwise inversion operators. The differences is very slight, but it is interesting.
x