TOTD #168: String switch statement - Using Java 7 in Java EE 6/GlassFish


Project Coin introduces a small set of changes to the Java language making your everyday programming easier. TOTD #167 explained Automatic Resource Management (ARM) and this Tip Of The Day (TOTD) will explain how to use Strings in switch statements.

Prior to JDK7, switch statement worked on byte, short, char, and int primitive data types and enum. It took us about 16 years to realize that java.lang.String is also a constant and add it to the list of data types supported by a switch statement :-)

Consider the following RESTful endpoint that responds with the color of a fruit:

@Path("fruits")
public class FruitResource {

@GET
@Produces("application/json")
@Path("{name}")
public String getJson(@PathParam("name")String name) {
if (name.equals("apple") || name.equals("cherry") || name.equals("strawberry"))
return "Red";
else if (name.equals("banana") || name.equals("papaya"))
return "Yellow";
else if (name.equals("kiwi") || name.equals("grapes") || name.equals("guava"))
return "Green";
else if (name.equals("clementine") || name.equals("persimmon"))
return "Orange";
else
return "Unknown";
}
Using JDK 7's String switch statement this code can be easily re-written as:

@Path("fruits")
public class FruitResource {

@GET
@Produces("application/json")
@Path("{name}")
public String getJson(@PathParam("name")String name) {
switch (name) {
case "apple":
case "cherry":
case "strawberry":
return "Red";
case "banana":
case "papaya":
return "Yellow";
case "kiwi":
case "grapes":
case "guava":
return "Green";
case "clementine":
case "persimmon":
return "Orange";
default:
return "Unknown";
}
}

The number of characters is pretty similar (669 using switch and 674 without) but the code is much more readable, no chained if-else statements, and slightly better performance (not measured). Couple of things to keep in mind:
  • Check for null before the switch
  • Check for different case letters before the switch

NetBeans 7.0.1 provide a hint if you have a chained if-else statement to take advantage of the JDK7 features. Have you downloaded it yet ? Check out screencast #35 highlighting Project Coin features in NetBeans.

NetBeans 7.0.1 provide complete tooling around JDK7 and GlassFish 3.1.1 allow you to leverage JDK7 features in your Java EE 6 applications.

How are you using JDK7 features in your Java EE 6 applications ?


Comments:

I saw the usage of String switch at my JUG, however IMHO it was poorly implemented.
For instance one of the first rules to avoid NullPointerException if to use a constant as the object on whom the equals method is called on.
For instance
"apple".equals(name) returns false if name is null.
while
name.equals("apple") throws an NPE if name is null.

the String switch should have worked as the first example enforcing NPE protection, but it does not.
Converting the old code to the new statement may bring problems.

Posted by Enrico Scantamburlo on August 08, 2011 at 02:27 AM PDT #

Post a Comment:
Comments are closed for this entry.
About

profile image
Arun Gupta is a technology enthusiast, a passionate runner, author, and a community guy who works for Oracle Corp.


Java EE 7 Samples

Stay Connected

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today