Using Strings in switch Statements (New in Java SE 7)
As of Java SE 7, you can use String
s in a switch
statement’s controlling expression and in case
labels. For example, you might want to use a city’s name to obtain the corresponding ZIP code. Assuming that city
and zipCode
are String
variables, the following switch
statement performs this task for three cities:
switch( city ) { case "Maynard": zipCode = "01754" ; break ; case "Marlborough": zipCode = "01752" ; break ; case "Framingham": zipCode = "01701" ; break ; } // end switch
Get Android™ How to Program, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.