BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Switch with Strings

Last Updated: June 9, 2024 by Chaitanya Singh | Filed Under: java

Introduced in Java 7, you can use switch statement with Strings. This makes code more readable as sometimes the string value of switch case variable makes more sense. Let’s see how can we achieve this:

Java Switch with Strings Examples

Example 1: Checking day of the week using switch case

public class SwitchStringExample {
public static void main(String[] args) {
String dayOfWeek = "Tuesday";

switch (dayOfWeek) {
case "Monday":
System.out.println("First day of the week");
break;
case "Tuesday":
System.out.println("Second day of the week");
break;
case "Wednesday":
System.out.println("Third day of the week");
break;
case "Thursday":
System.out.println("Two days to go for weekend");
break;
case "Friday":
System.out.println("A day before weekend");
break;
case "Saturday":
case "Sunday":
System.out.println("Weekend");
break;
default:
System.out.println("The data is invalid");
break;
}
}
}

Explanation:

  1. Variable: The value of the variable dayOfWeek determines which case is to be executed.
  2. Switch Statement: The switch statement evaluates the value of dayOfWeek, the value can be an expression.
  3. Case Labels: Case label is a string literal in this example. When the value of dayOfWeek matches a case label, the corresponding block of code is executed.
  4. Break Statement: After the execution of case block, the break statement terminates the further execution.
  5. Default Case: The default case is executed if none of the other cases match.

Points to Note:

  • Case Sensitivity: The comparison of variable and case literal is case-sensitive, which means “Tuesday” and “tuesday” would not be considered a match.
  • Efficiency: The switch statement with strings internally uses hashCode() and equals()methods of the String class, making it relatively efficient.
  • Null Values: If the value of String variable is null, a NullPointerException will be thrown. It is always advisable to ensure that the string is not null before using it in a switch statement.

Example 2: Determining Type of Beverage

public class BeverageTypeExample {
public static void main(String[] args) {
String beverage = "Tea";

switch (beverage) {
case "Coffee":
case "Tea":
System.out.println("Hot Beverage");
break;
case "Juice":
case "Soda":
System.out.println("Cold Beverage");
break;
case "Water":
System.out.println("Normal Beverage");
break;
default:
System.out.println("Unknown Beverage Type");
break;
}
}
}

Top Related Articles:

  1. String Concatenation in Java
  2. If, If..else Statement in Java with Examples
  3. Continue Statement in Java with example
  4. What is a boolean Keyword in Java
  5. What is byte Keyword in Java

Tags: Java-Strings

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

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

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap