Vineet Manohar is CTO / Co-Founder of online Car Search Engine, iSeeCars.com. Vineet is a DZone MVB and is not an employee of DZone and has posted 20 posts at DZone. You can read more from them at their website. View Full User Profile

New Java 7 Feature: String in Switch support

03.22.2011
| 18380 views |
  • submit to reddit

One of the new features added in Java 7 is the capability to switch on a String.

With Java 6, or less

    String color = "red";  

if (color.equals("red")) {
System.out.println("Color is Red");
} else if (color.equals("green")) {
System.out.println("Color is Green");
} else {
System.out.println("Color not found");
}
 String color = "red";

 if (color.equals("red")) {
   System.out.println("Color is Red");
 } else if (color.equals("green")) {
   System.out.println("Color is Green");
 } else {
   System.out.println("Color not found");
 }

With Java 7:

String color = "red";  

switch (color) {
case "red":
System.out.println("Color is Red");
break;
case "green":
System.out.println("Color is Green");
break;
default:
System.out.println("Color not found");
}
Conclusion

The switch statement when used with a String uses the equals() method to compare the given expression to each value in the case statement and is therefore case-sensitive and will throw a NullPointerException if the expression is null. It is a small but useful feature which not only helps us write more readable code but the compiler will likely generate more efficient bytecode as compared to the if-then-else statement.

 

From http://www.vineetmanohar.com/2011/03/new-java-7-feature-string-in-switch-support/

Published at DZone with permission of Vineet Manohar, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Tomasz Nurkiewicz replied on Tue, 2011/03/22 - 2:04am

"compiler will likely generate more efficient bytecode as compared to the if-then-else statement" - see this. Actually I believe this approach is described in the specification itself.

Piotr Kochanski replied on Tue, 2011/03/22 - 6:35am

String in switch does not sound as a great idea since it promotes hardcoding Strings in the application, which is a very bad practice. I'll be extremly surprised if I ever need to use that feature.

Java needs true enhancements, like sane properties aka getters and setters syntax (yes, it is doable Oracle, see http://projectlombok.org).

Jonathan Fisher replied on Tue, 2011/03/22 - 10:34am

Yeah, but in rubypython mamby pamby land you can switch on Strings. So Java needs to switch on Strings.

Slava Imeshev replied on Tue, 2011/03/22 - 3:37pm

Switching on Strings is long overdue. Though most of us have learned to use enums when needed to switch on non-integer literals, String switches will come handy in many cases.

Regards,

Slava Imeshev
Cacheonix - Reliable Distributed Cache

darryl west replied on Tue, 2011/03/22 - 10:46pm

switch with strings == big win...finally.

Jon Nichols replied on Wed, 2011/03/23 - 4:44am

Why not switch on object? The default implementation could just use .equals(), with compilers optimizing for more efficient usage.

Dirk Hillbrecht replied on Wed, 2011/03/23 - 6:00am

Well, if only they would overcome that silly "break"-syntax somewhen. That's by far the most annoying syntax quirk Java got from it's C-syntax-roots...

Jessie Mear replied on Wed, 2011/09/07 - 6:46am

JDK 7 has made an important enhancement in there support of String, now you can use String in switch and case statement, No doubt String is most widely used type in Java and in my opinion they should have made this enhancement long back when they provided support for enum in java and allowed enum to be used in switch statement. java programmers

Raghvandra Shukla replied on Thu, 2014/01/16 - 1:14am

The implementation of switch over string is more interesting. I found this article with a good explanation.

Features from Java-7 that you will love being a developer


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.