Switch to full style
What is new in JDK7
Post a reply

Use Switch with strings

Mon Nov 05, 2012 4:03 pm

You can use Switch with strings at java 7 as follows:
Code:


public class StringsWithSwitch {

    public static 
void main(String args[]) {

        
String City "Paris";
        
String CountryInfo "";

        switch (
City) {
            case 
"Madrid":
                
CountryInfo "You are in Spain";
                break;
            case 
"Cairo":
                
CountryInfo "You are in Egypt";
                break;
            case 
"Paris":
                
CountryInfo "You are in France";
                break;

            case 
"Berlin":
                
CountryInfo "You are in Germeny";
                break;
            case 
"London":
                
CountryInfo "You are in Britain";
                break;

            default:
                throw new 
IllegalArgumentException("Don't know your country");
        }
        
System.out.println(CountryInfo);
    }
}
 


The output is :
Code:
You are in France




Post a reply
  Related Posts  to : Use Switch with strings
 Sort strings java-Sorting Array of Strings     -  
 Switch vs Hub     -  
 php switch     -  
 network switch     -  
 JavaScript switch example     -  
 java switch example     -  
 switch keyword in c++ usage     -  
 switch command for string value     -  
 PHP Strings     -  
 C++ Strings     -  

Topic Tags

Java Strings