Conditional Operator

The only ternary operator available in java is conditional operator.

int a = ( 10<20 ) ? 30 : 40 ;

System.out.println( a) ; // 30

Nesting of conditional operator is also allowed in java.

  • int x = ( 100>200 ) ? 10 : ( (1000>3000)  ? 20 : 30 ) ; System.out.println( x) ; // 30
  • int a = 10, b = 20 :
  • byte c = ( false) ? 30 : 40 ;
  • byte c = ( 10>20 ) ? 30 : 40 ;
  • byte c = ( 10<20 ) ? 30 : 40 ;
  • byte c = ( a>b ) ? 30 : 40 ; // P.L.P
  • byte c = ( a<b ) ? 30 : 40 ;   // P.L.P

(a<b) if it is not compile tie compression than it check for type, not values directly  assigned value ( 30, 40 ) of types.

 final int a = 10 ; b = 20 ; 
byte c = ( a<b ) ? 30 : 40 ;
System.out.println (c) ; // 30

Leave a Reply