special_operators.java
1 //Special Operators 2 3 public class special_operators { 4 /* 5 * Ternary Operator `?:` 6 * `instanceof` Operator 7 */ 8 9 public static void main(String[] args) { 10 /* 11 * Ternary Operator is used for boolean operations 12 * Can be used as a one-line replacement for if-elif-else operations 13 */ 14 15 int a = 10; 16 Integer b = 20; 17 18 System.out.println("\nWith if-else ladder"); 19 if (a % 2 == 0) { 20 System.out.println(a + " is Even"); 21 22 } 23 else { 24 System.out.println(a + " is Odd"); 25 } 26 27 System.out.println("\nUsing Ternary Operator"); 28 29 System.out.println(a + " is " + (a % 2 == 0 ? "Even" : "Odd")); 30 31 /* 32 * instanceof operator is used to find if an object is an instance of a specific class 33 * Make sure the input is an object or a wrapper class 34 */ 35 36 System.out.println("\ninstanceof Operator"); 37 System.out.println(b instanceof Integer); 38 39 } 40 }