x |
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 fiveThe Boolean Complement Operator:
boolean flag = false; flag = !flag; // changes value of flag to trueThe Cast Operator:
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:
x |
x |