Prev Next

Java / Using String

Can we use String in switch case control statement?

Java 7 featured the capability of switch case to use Strings, earlier java versions haven't supported it.

Example

package org.javatutorials.conditional;

public class SwitchCaseUsingString {

	public static void main(String[] args) {

		String stringObj = "ONE";
		
		switch (stringObj) {
		case "ONE":
				System.out.println("1 is entered"); break;
		case "TWO":
			System.out.println("2 is entered"); break;
		case "THREE":
			System.out.println("3 is entered"); break;
		default:
			System.out.println("Something else is entered");
		}
		
	}

}

It's right time to invest in Cryptocurrencies Dogecoin! Earn free bitcoins up to $250 now by signing up.

Earn bitcoins upto $250 (free), invest in other Cryptocurrencies when you signup with blockfi. Use the referral link: Signup now and earn!

Using BlockFi, don't just buy crypto - start earning on it. Open an interest account with up to 8.6% APY, trade currencies, or borrow money without selling your assets.


Join CoinBase! We'll both receive $10 in free Bitcoin when they buy or sell their first $100 on Coinbase! Available in India also. Use the referral Join coinbase!


Invest now!!! Get Free equity stock (US, UK only)!

Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.

The Robinhood app makes it easy to trade stocks, crypto and more.


Webull! Receive free stock by signing up using the link: Webull signup.

More Related questions...

How do I iterate through every character in a String? (or) How to parse each character in a String object? toCharArray() Method How do I convert a char (primitive data type variable) to string in Java? Can we use String in switch case control statement? How do I split a pipe delimited Java String using split method? How do I create a Regular expression pattern into non-greedy in Java? Explain SimpleDateFormat class and its usage in Java. How to check if a string is not null and not empty in Java? How do I remove duplicate elements from a String Array in Java? How do I write a new line character to a file in Java? What is the maximum length of a String in Java? How many String objects be created by the snippet and where it is stored in Java? How do you convert a string object to character array in Java? Is string a wrapper class in Java? How many spaces does the Java String.trim() remove? Can we compare String and StringBuffer objects in Java? Which class is final:String, StringBuffer and StringBuider? What is the initial capacity of a StringBuilder object in Java? How to avoid NullPointerException when comparing two Strings in Java? Does substring method cause memory leak? what happens if beginIndex is equal to length of the String when calling substring(int beginIndex)? When substring method throws IndexOutOfBoundException? How to resolve substring memory leak issue in java version prior to JDK 1.7? Where String pool is located? What is the output of this statement?
System.out.println ("A"=="A");
What is the difference between appending a string to a StringBuilder and concatenating two strings with a + operator? How string concatenation operator (+) works in Java? When the concatenation of final string literal happen? Is Java's SimpleDateFormat thread-safe?
Show more question and Answers...

Exception

Comments & Discussions