Home > Tutorial > Java Switch Statement Sample Code

Java Switch Statement Sample Code

The Java switch statement is useful when you need to make a decision in a program with multiple possibilities. While the same principle can be done with a series of if-else statements, the switch statement is easier to read.

You can use many different data types in the switch expression, which includes: byte, short, int, char, and String as of Java 1.7 version.

This Java code sample will focus on using an int and String for the switch expression statement. It’s also a good idea to end each switch expression with a break statement, which terminates the expression, but I used a return at the end of each expression instead.

Java Switch Statement Source Code:

package com.jcd.java.tutorials;

public class JavaSwitchStatement 
{

	public static void main(String[] args) 
	{
		
		System.out.println("Days In March: "+numberDaysInMonth("MARCH"));
		System.out.println("Days In July: "+numberDaysInMonth("july"));
		System.out.println("Days In November: "+numberDaysInMonth("November"));
		System.out.println("Days In Dummy: "+numberDaysInMonth("Dummy"));
		
		System.out.println("\n---------------------------------------------------");
		
		System.out.println("\nDays In Dummy: "+returnMonthName(1));
		System.out.println("Days In Dummy: "+returnMonthName(5));
		System.out.println("Days In Dummy: "+returnMonthName(2));
		System.out.println("Days In Dummy: "+returnMonthName(12));
		
		
	}
	
	//Method using switch to return number of days in a month
	//Input = String | Return = int
	public static int numberDaysInMonth(String month) 
	{
		
		switch(month.toLowerCase()) 
		{
		
		case "january" :
            return 31;
            
        case "february" :
        	return 28;

        case "march" :
        	return 31;

        case "april":
        	return 30;

        case "may":
        	return 31;

        case "june":
        	return 30;
  
        case "july":
        	return 31;
         
        case "august":
        	return 31;
            
        case "september":
        	return 30;
           
        case "october":
        	return 31;
          
        case "november":
        	return 30;
          
        case "december":
        	return 31;
     
        default: 
            return 0;
		}
		
	}
	
	//Sample Code Using Switch Statement With String
	//Input = int | Return = String
	public static String returnMonthName(int i) 
	{
		
		switch(i) 
		{	
			case 1:
				return "January";
			case 2:
				return "February";
			case 3:
				return "March";
			case 4:
				return "April";	
			case 5:
				return "May";	
			case 6:
				return "June";	
			case 7:
				return "July";	
			case 8:
				return "August";
			case 9:
				return "September";
			case 10:
				return "October";	
			case 11:
				return "November";	
			case 12:
				return "December";	
			default:
				return "N/A";
		}
		
	}

}



Sample Java Switch Statement Output:

Days In March: 31
Days In July: 31
Days In November: 30
Days In Dummy: 0

---------------------------------------------------

Days In Dummy: January
Days In Dummy: May
Days In Dummy: February
Days In Dummy: December



References:

Seymour
Posted by

Seymour

I have been a software developer for twenty years now focusing on writing high quality Java applications. If you are in a programming field, you know that technology is constantly changing. You have to keep current or you will get left behind!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © 2018 JavaCodeDepot