Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

Switch statement

The switch statement has more alternative execution routes than the if and if-else commands. The primitive data types byte, short, char, and int are all compatible with switches. It also functions with enumerated types, the String class, and a couple of special classes that encase a few primitive types including Character, Byte, Short, and Integer.

Important points on switch statement

Switch statement

  • Java uses switch statements to allow the execution of a single statement under various conditions. Variables of many types, including short, byte, int, long, enum, and others, can be used with the switch statement.
  • The case value must be of switch expression type only.For example, if a switch statement evaluates an integer expression, the case values must be integers as well. If the switch expression is a string, so should the case values be.
  • The break statement is used to stop the execution of chain of statements. It's optional to use break keyword. If a break statement is not found after any paticular case then it will executes the next case.
  • Similar case values are not allowed to be used. If unique values are not utilised, the compiler issues a compile-time error.
  • Since flow is excluded from the switch statement, the final break is not technically necessary. To make editing the code simpler and less prone to error, it is advised to use breaks.
  • The default label for the case value is an optional feature.
  • You have the option to give one or N case values for a switch expression.



Syntax

 switch  (expression){  
 case  value1: 
     statement 1;  // your code  execute when condition 2 is true 
     break;   
 case  value2: 
     statement 2;  // your code  execute when condition 2 is true 
     break;   
  default:   
     statement 2;  // your code  execute when condition is false 
  } 
  } 

Flow Chart

 // Decision-Making program using switch case statement  
public class Student { 
	public static void main(String[] args) {  	      
        int month = 10;
        String monthName;
        switch (month) {
            case 1:  monthName = "January";
                     break;
            case 2:  monthName = "February";
                     break;
            case 3:  monthName = "March";
                     break;
            case 4:  monthName = "April";
                     break;
            case 5:  monthName = "May";
                     break;
            case 6:  monthName = "June";
                     break;
            case 7:  monthName = "July";
                     break;
            case 8:  monthName = "August";
                     break;
            case 9:  monthName = "September";
                     break;
            case 10: monthName = "October";
                     break;
            case 11: monthName = "November";
                     break;
            case 12: monthName = "December";
                     break;
            default: monthName = "Please Enter Valid Month number";
                     break;
        }
        System.out.println(monthName);
    }  
} 


Output:

October 

Java Enum in Switch Statement

Let's learn about enum from below point

  • By default, enum constants are final, static, and public.
  • Only interfaces can be implemented by an enum class.
  • In addition to constants, an enum class can also have attributes and methods.
  • Dot syntax is used to access enum constants.
  • Enum classes cannot extend other classes and cannot be used to generate new objects.

 // Java Enum program in Switch Statement  
public class Student { 
	enum DayEnum {Sun, Mon, Tue, Wed, Thu, Fri, Sat}  
	public static void main(String[] args) {  		  
		DayEnum DaysName = DayEnum.Sun;       
         switch (DaysName)    
         {    
             case Sun:    
                 System.out.println("Sunday");    
                 break;    
             case Mon:    
                 System.out.println("Monday");    
                 break;    
             case Tue:    
                 System.out.println("Tuesday");    
                 break;         
             case Wed:    
                 System.out.println("Wednesday");    
                 break;    
             case Thu:    
                 System.out.println("Thursday");    
                 break;    
             case Fri:    
                 System.out.println("Friday");    
                 break;    
             case Sat:    
                 System.out.println("Saturday");    
                 break;                 	 
         }                
    }  
}  

Output:

Sunday 

Java Switch Statement without break statement

 // Java Enum program in Switch Statement  
public class Student { 
	enum DayEnum {Sun, Mon, Tue, Wed, Thu, Fri, Sat}  
	public static void main(String[] args) {  		  
		DayEnum DaysName = DayEnum.Sun;       
         switch (DaysName)    
         {    
             case Sun:    
                 System.out.println("Sunday");    
                
             case Mon:    
                 System.out.println("Monday");    
                
             case Tue:    
                 System.out.println("Tuesday");    
                  
             case Wed:    
                 System.out.println("Wednesday");    
                 
             case Thu:    
                 System.out.println("Thursday");    
                
             case Fri:    
                 System.out.println("Friday");    
                 
             case Sat:    
                 System.out.println("Saturday");    
                          	 
         }                
    }  
}

Output:

Sunday 
Monday 
Tuesday 
Wednesday 
Thursday 
Friday 
Saturday 
java for loop Next »
« Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
Java Control Statements
java if else
java switch
java for loop
java While loop
java Do While loop
java break
java continue
Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.