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

The Author

Narayanaswamy

Hello! I am Narayanaswamy founder and admin of narayanatutorial.com. I have been working in IT industry more than 7 years. NarayanaTutorial is my web technologies blog. My specialties are Java / J2EE, Spring, Hibernate, Struts, Webservices, PHP, Oracle, MySQL, SQLServer, Web Hosting and Website Development. I am a self learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.

Leave a Reply