Operator | Precedence |
---|---|
postfix | expr++ expr-- |
unary | ++expr --expr +expr -expr |
arithmetic | * / % + - |
shift | << >> >>> |
Relational | < > <= >= instanceof |
equality | == != |
bitwise | & ^ | |
logical | && | | |
conditional | ? : |
assignment | = += -= *= /= %= &= ^= |= <<= >>= >>>= |
class ArithmeticDemo
{
public static void main(String args[])
{
int num1 = 15, num2 = 6;
System.out.println( "num1 + num2 : " + (num1+num2) );
System.out.println( "num1 - num2 : " + (num1-num2) );
System.out.println( "num1 * num2 : " + (num1*num2) );
System.out.println( "num1 / num2 : " + (num1/num2) );
System.out.println( "num1 % num2 : " + (num1%num2) );
}
}
class IncreDecreDemo
{
public static void main(String[] args)
{
int i = 5;
i++
System.out.println(i); // prints 6
++i;
System.out.println(i); // prints 7
System.out.println(++i); // prints 8
System.out.println(i++); // prints 8
System.out.println(++i); // prints 10
}
}
class RelationalDemo
{
public static void main(String args[])
{
int num1 = 15;
int num2 = 30;
System.out.println("num1 == num2 = " + (num1 == num2) );
System.out.println("num1 != num2 = " + (num1 != num2) );
System.out.println("num1 > num2 = " + (num1 > num2) );
System.out.println("num1 < num2 = " + (num1 < num2) );
System.out.println("num1 >= num2 = " + (num1 >= num2) );
System.out.println("num1 <= num2 = " + (num1 <= num2) );
}
}
class BitDemo
{
public static void main(String args[])
{
int x = 5;
int y = 3;
System.out.println("x | y = "+(x | y));
System.out.println("x & y = "+(x & y));
System.out.println("x ^ y = "+(x ^ y));
System.out.println("x >> 1 = "+(x >> 1));
System.out.println("x << 1 = "+(x << 1));
System.out.println("x >>> 1 = "+(x >>> 1));
}
}
class LogicalTest
{
public static void main(String args[])
{
boolean b1 = false;
boolean b2 = true;
System.out.println("b1 && b2 = " + (b1&&b2));
System.out.println("b1 || b2 = " + (b1||b2) );
System.out.println("! (b1 && b2) = " + ! (b1 && b2));
}
}
class MaxMinTest
{
public static void main(String args[])
{
int a = 15;
int b = 30;
int c = 25;
int max = (a > b & a > c) ? a : (b > c) ? b : c;
int min = (a<b & a<c) ? a : (b<c) ? b : c;
System.out.println("Maximum value is: "+max);
System.out.println("Minimum value is: "+min);
}
}