1. Arithmetic Operators Example
public class Arithmetic {public static void main(String args[]) {
int a = 10;
int b = 20;
int c = 25;
int d = 25;
System.out.println("a + b = " + (a + b) );
System.out.println("a - b = " + (a - b) );
System.out.println("a * b = " + (a * b) );
System.out.println("b / a = " + (b / a) );
System.out.println("b % a = " + (b % a) );
System.out.println("c % a = " + (c % a) );
System.out.println("a++ = " + (a++) );
System.out.println("b-- = " + (a--) );
// Check the difference in d++ and ++d
System.out.println("d++ = " + (d++) );
System.out.println("++d = " + (++d) );
}
}
Output :
a + b = 30, a - b = -10, a * b = 200, b / a = 2, b % a = 0, c % a = 5, a++ = 10, b-- = 11, d++ = 25, ++d = 272. Relational Operators Example
public class Relational {public static void main(String args[])
int a = 10;
int b = 20;
System.out.println("a == b = " + (a == b) );
System.out.println("a != b = " + (a != b) );
System.out.println("a > b = " + (a > b) );
System.out.println("a < b = " + (a < b) );
System.out.println("b >= a = " + (b >= a) );
System.out.println("b <= a = " + (b <= a) );
}
}
Output :
a == b = false, a != b = true, a > b = false, a < b = true, b >= a = true, b <= a = false3. BitWise Operators Example
public class BitWise {public static void main(String args[]) {
int a = 60;
int b = 13;
int c = 0;
c = a & b;
System.out.println("a & b = " + c );
c = a | b;
System.out.println("a | b = " + c );
c = a ^ b;
System.out.println("a ^ b = " + c );
c = ~a;
System.out.println("~a = " + c );
c = a << 2;
System.out.println("a << 2 = " + c );
c = a >> 2;
System.out.println("a >> 2 = " + c );
c = a >>> 2;
System.out.println("a >>> 2 = " + c );
}
}
Output :
a & b = 12, a | b = 61, a ^ b = 49, ~a = -61, a << 2 = 240, a >> 15, a >>> 154. Logical Operators Example
public class Logical {public static void main(String args[]) {
boolean a = true;
boolean b = false;
System.out.println("a && b = " + (a&&b));
System.out.println("a || b = " + (a||b) );
System.out.println("!(a && b) = " + !(a && b));
}
}
Output :
a && b = false, a || b = true, !(a && b) = true5. Assignment Operators Example
public class Assignment {public static void main(String args[]) {
int a = 10;
int b = 20;
int c = 0;
c = a + b;
System.out.println("c = a + b = " + c );
c += a;
System.out.println("c += a = " + c );
c -= a;
System.out.println("c -= a = " + c );
c *= a;
System.out.println("c *= a = " + c );
a = 10;
c = 15;
c /= a;
System.out.println("c /= a = " + c );
a = 10;
c = 15;
c %= a;
System.out.println("c %= a = " + c );
c <<= 2;
System.out.println("c <<= 2 = " + c );
c >>= 2;
System.out.println("c >>= 2 = " + c );
c >>= 2;
System.out.println("c >>= a = " + c );
c &= a;
System.out.println("c &= 2 = " + c );
c ^= a;
System.out.println("c ^= a = " + c );
c |= a;
System.out.println("c |= a = " + c );
}
}
Output :
c = a + b = 30, c += a = 40, c -= a = 30, c *= a = 300, c /= a = 1c %= a = 5, c <<= 2 = 20, c >>= 2 = 5, c >>= 2 = 1, c &= a = 0, c ^= a = 10, c |= a = 10
6. Conditional Operators Example
public class Conditional {public static void main(String args[]){
int a, b;
a = 10;
b = (a == 1) ? 20: 30;
System.out.println( "Value of b is : " + b );
b = (a == 10) ? 20: 30;
System.out.println( "Value of b is : " + b );
}
}