Back to articles
#20 Known is a drop! Switch statement & Switch Expression JAVA-Tricky Questions

#20 Known is a drop! Switch statement & Switch Expression JAVA-Tricky Questions

via Dev.to TutorialDeepikandas

1 . public class Sample { public static void main ( String [] args ) { int x = 2 ; switch ( x ) { case 1 : System . out . println ( "One" ); break ; case 2 : System . out . println ( "Two" ); break ; case 3 : System . out . println ( "Three" ); break ; default : System . out . println ( "Default" ); } } } output: Two //After java 8th version switch case : 2 . public class Sample { public static void main ( String [] args ) { int x = 2 ; switch ( x ) { case 1 -> System . out . println ( "Hi" ); case 2 -> System . out . println ( "Welcome" ); default -> System . out . println ( "Hello" ); } //no need of semicolon as switch expression is not used } } output: Welcome public class Sample { public static void main ( String [] args ) { int x = 2 ; int result = switch ( x ) { case 1 -> 10 ; case 2 -> 20 ; default -> 0 ; }; //semicolon must System . out . println ( result ); } } output: 20 public class Sample { public static void main ( String [] args ) { int x = 1 ; int result = switch ( x ) {

Continue reading on Dev.to Tutorial

Opens in a new tab

Read Full Article
2 views

Related Articles