mercoledì 12 febbraio 2014

String case

Before Java SE 7 only numbers (byte, short, int, long, char) or enumerations could be used in switch cases. It is now possible to use a switch on a Strcompare alphanumerical values. This avoids long lists of if/then/else and makes the code more readable. The following example shows you what you can now write in your applications.

Example: A String Case
String action = "update";

switch (action) {

case "create":
create();
break;

case "read":
read();
break;

case "udpate":
udpate();
break;

case "delete":
delete();
break;

default:
OtherAction(action);
}

Nessun commento:

Posta un commento