Arithmetic Operators
+ (addition)
- (subtraction)
* (multiply)
/ (division)
% (modulus)
++ (increment) x++ x = x + 1;
-- (decrement) x-- x = x – 1;
Lets See a program:
class test
{
public static void main (String args[])
{
byte b = 12 * 6;
System.out.println (b);
int i1 = 12 + 13;
System.out.println (i1);
int i2 = 12 - 13;
System.out.println (i2);
int i3 = 12 / 13;
System.out.println (i3);
double d = 12d / 13d;
System.out.println (d);
int i4 = 12 % 3;
System.out.println (i4);
int i5 = 12 % 5;
System.out.println (i5);
}
}
Result is
72
25
-1
0
0.9230769230769231
0
2
Shortcut Operators
k = k + 2 k += 2
k = k – 2 k -= 2
k = k * 2 k *= 2
k = k / 2 k /= 2
k = k % 2 k %= 2
No comments:
Post a Comment