other languages. All operators can be grouped into 4 categories
1 Arithmetic
2 Bitwise
3 Relational
4 Boolean Logical
Arithmetic operators
- addition +
- subtraction -
- division /
- Multiplication *
- Modulus %
10 % 3 will return 1
Each of the above has shorthand notation also
a = a + b ;
a += b ;
Both are equivalent
Increment and decrement operators
++ Single unit increment operator
-- Single unit decrement operator
Above two have two flavours : Post fix and Pre fix
Lets see what's the diference between two by working on a simple example
int a = 5 ;
int b ;
Post fix
b = a ++ ; // b will be set to 5
System.out.println ("b = " + b + " a = " + a ) // b will be 5 and a will be 6
Here, the value of a is used first (assigned to b) and then it is incremented
Pre fix
b = ++a ; // b will be set to 6
System.out.println ("b = " + b + " a = " + a ) // b will be 6 and a will be 6
Here the value of a is first incremented and then used (assigned to b)
In both cases, a will finally be 6 but value of b is different in both cases.
Decrement ( -- ) operators work in the same way, they decrease the value by 1