We are moved to new domain
Click -> www.ehowtonow.com
Saturday, 7 December 2013

Java 7 – Switch statement on String


In this tutorials we are going to see old switch statement and new switch statement in java which support String argument.
Previous versions of java was not supported  String in switch statement.To achieve this functionality we go for if-else statement.The ability to switch on string is now supported and produce more efficient bytecode than if-else statement
Examples of old switch statement and if-else statement and String to switch to achieve the same result of if-else in more efficient way is given below
Old switch Statement
switch statement on integer value
package com.javatutorialscorner.java7.switchcase;

public class OldSwitchStatement {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("First Call : " + getWeek(2));
System.out.println("Second Call : " + getWeek(5));
System.out.println("Default Call : " + getWeek(0));
}

public static String getWeek(int day) {
String week;
switch (day) {
case 1:
week = "Sunday";
break;
case 2:
week = "Monday";
break;
case 3:
week = "Tuesday,";
break;

case 4:
week = "Wednesday";
break;

case 5:
week = "Thursday,";
break;

case 6:
week = "Friday";
break;

default:
week = "Saturday";
break;
}
return week;
}
}

output

First Call : Monday

Second Call : Thursday,


Default Call : Saturday

if-elseif-else statement

package com.javatutorialscorner.java7.switchcase;

public class IfElseIf {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(getLanguage("java"));
System.out.println(getLanguage("html"));
System.out.println(getLanguage(""));
}

public static String getLanguage(String lang) {
String result;
final String JAVA = "java";
final String HTML = "html";
final String CPP = "c++";
if (lang == null) {
return "null value";
}
if (lang.equals(JAVA)) {
result = "This is Java";
} else if (lang.equals(HTML)) {
result = "This is HTML";
} else if (lang.equals(CPP)) {
result = "This is C++";
} else {
result = "Language not found";
}
return result;
}
}

output

This is Java

This is HTML

Language not found

New switch statement

package com.javatutorialscorner.java7.switchcase;

public class NewSwitchStatement {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(getLanguage("java"));
System.out.println(getLanguage("html"));
System.out.println(getLanguage(""));
}

public static String getLanguage(String lang) {
String result;
final String JAVA = "java";
final String HTML = "html";
final String CPP = "c++";
if (lang == null) {
return "null value";
}
switch (lang) {
case JAVA:
result = "This is Java";
break;
case HTML:
result = "This is HTML";
break;
case CPP:
result = "This is C++";
break;
default:
result = "Language not found";
break;
}
return result;
}

}

output

This is Java

This is HTML

Language not found

In above code if condition is used to avoid null pointer expression.

The switch statement compare the string object in its expression with the expressions associated with each case label .The Java compiler generates more efficient byte code from switch statements that use String objects than from chained if- else statement.

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Java 7 – Switch statement on String Rating: 5 Reviewed By: eHowToNow