Objectives:
- How to use switch case in Java?
- How to use switch case with string in Java?
- How to pass string literals in switch case in Java?
Source Code:
[sourcecode lang=”java”]
public class SwitchWithStringExample
{
public static void main(String[] args)
{
String str = "two";
switch(str)
{
case "one":
System.out.println("one");
break;
case "two":
System.out.println("two");
break;
case "three":
System.out.println("three");
break;
default:
System.out.println("no match");
} // switch close
} // main close
} // class close
[/sourcecode]
Steps to run above Java code:
Consider that we have saved code at C drive (c:\>) and the file is saved as SwitchWithStringExample.java
- Compilation : c:\> javac SwitchWithStringExample.java
- Interpretation : c:\> java SwitchWithStringExample
Output:
two
We conduct Java Training and Workshop for Corporates and College Students, if interested, please write to us on kloudsancyber@gmail.com
Aatul Palandurkar
International Trainer, and Author, NetBeans Dream Team Member
https://www.facebook.com/aatulpalandurkar
https://www.instagram.com/aatulpalandurkar/