Java 7 can make use of String type in switch statement

14 11 2015

What types you can use in cases in switch block?

  • before Java 7: enum, byte, char, short, int (or their reference-types equivalence: Byte, Char, Short, Integer)
  • since Java 7 you can also use String

Now the switch statement like this one is fully legal:

switch (commandToHandle){
  case "stop": killService(); break;
  case "start": startService(); break;
  case "restart": restartService(); break;
  default: displayHelp(); break;

This is a small improvement in the Java Language Standard (JLS). It does not touch the JVM at all, since switch case type is compiled by javac while building the .class files. .class file format remains unchanged so there is no need to modify JVM or VMSpec in any way



Give Your feedback:

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: