Java switch case with string

Switch statement existed before Java 7 as well, but it supported only int and enum types. After Java 7 release, Switch statement support String class also.

1. Java switch case with string

Java program to use String class in switch case statements.

public class StringSupportedInSwitch 
{
	public static void main(String[] args)
	{
		System.out.println(getExpendedMessage("one"));
		System.out.println(getExpendedMessage("three"));
		System.out.println(getExpendedMessage("five"));
	}

	static String getExpendedMessage(final String token) 
    {
        String value = null;
        
        switch (token) 
        {
            case ("one"):
                value = "Token one identified";
                break;
    
            case ("two"):
                value = "Token two identified";
                break;
    
            case ("three"):
                value = "Token three identified";
                break;
    
            case ("four"):
                value = "Token four identified";
                break;
    
            default:
                value = "No token was identified";
       }
        
        return value;
    }
}

Program Output.

Token one identified
Token three identified
No token was identified

2. Java switch case handle multiple conditions

Sometimes, we want to perform certain action on multiple cases in switch statement. In this case, we can write each value in separate case and only after all cases are written, write down application logic.

For example, in given program all odd tokens will be handled by first switch case, and even tokens will be handled by second switch case.

Java example of switch case with multiple conditions.

public class StringSupportedInSwitch {

	public static void main(String[] args) 
	{
		System.out.println(getExpendedMessage("one"));
		System.out.println(getExpendedMessage("two"));
	}

	static String getExpendedMessage(final String token) 
	{
		String value = null;

		switch (token) 
		{
			case ("one"):
			case ("three"):
				value = "Odd token identified";
				break;
	
			case ("two"):
			case ("four"):
				value = "Even token identified";
				break;
	
			default:
				value = "No token was identified";
		}

		return value;
	}
}

Program Output.

Odd token identified
Even token identified

In this example, we learned to use Java switch statement with strings. This feature was introduced in Java 7.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

2 thoughts on “Java switch case with string”

  1. 2. Java switch statement case handle multiple values

    Code snippet was incorrect for case 2, seems first code itself copied over there too. Please correct it.

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.