String in Switch (Java 7)
Based on your language feature String in Switch (Java 7), you will write a technical report with more than 500 words, including below:
Give an example of the usage of the language feature
Enumerate pros and cons
What benefits does the feature offer? For example,
LOC (Lines of Code) can be reduced by using that language feature.
Program readability can be improved.
Create a research question (RQ) and than answers

Add references used.
String in Switch (Java 7)
Java switch case is a neat way for conditional flow coding by supporting the String. The String in Java 7 switch performs condition flow without using if or else conditions. The string literal or constant can be used to control a switch statement which is not applicable when using C/C++.
Example of the String in Java 7 switch is provided bellow.
// Java program to demonstrate use of a
// string to control a switch statement.
public class Test
{
public static void main(String[] args)
{
String str = “two”;
switch(str)
{
case “one”:
System.out.println(“one”);
break;
case “two”:
System.out.println(“two”);
break;
case “three”:
System.out.println(“three”);
break;
default:
System.out.println(“no match”);
}
}
}

The switch statement compares the String object by comparing the expression of the sting object with the expression associated with each case label as if the String.equals method was being used (GeeksforGeeks, 2019). Comparing of string objects in switch statements is extremely case sensitive. More efficient bytecode is generated using Java compiler from switch statement that uses string objects than those from chained if-then-else statements. When working with strings, it is essential to ensure that the expression in any switch statement is not null in order to prevent the NullPointerException from being thrown at run-time.
The String in switch feature has several advantages as well as disadvantages. The advantages of the String in Java 7 switch include making the code more readable by removing the multiple if-else-if chained conditions (JournalDev, 2019). Another advantage of Strings in the switch is that it enables the generation of more efficient byte code using Java compiler than chained if-else-if statements. The destination of the strings in a switch statement can be easily computed by looking at the table. Other advantages include the potential for faster execution, easier to debug and maintain. When it comes to the disadvantages of the strings in switch feature, the most common is an expensive operation. It is more costly in terms of execution when switching on strings than switching o primitive data types. The expensive operation forces the users to the only switch on strings only in cases that controlling data is already in string form, which creates a disadvantage of lack of flexibility. Another disadvantage of the feature is that it does not support variable expression in case. In the string in the switch, the float constant cannot be applied in the switch as well as in the case. Moreover, the same constant cannot be used in two different cases. Another con for the string in switch feature is that it does not support the use of a rational expression in case.
The benefits of String in a switch include improving the performance for string-based dispatch code. Whereby the feature allows regular coding patterns to be used for the selected operations based on a set of constant string values. The feature is also beneficial since it improves the efficiency of writing more readable code (Pece, 2011). Another benefit is that the feature helps in the generation of more efficient bytecode by the compiler. The feature enables the reduction of Lines of Code (LOC) which makes writing and reading of the code much easier. The String in switch feature enables the use of case number in any random order.

References
GeeksforGeeks. (2019). String in Switch Case in Java. Retrieved from https://www.geeksforgeeks.org/string-in-switch-case-in-java/
JournalDev. (2019). Java switch case String. Retrieved from https://www.journaldev.com/588/java-switch-case-string
Pece, S. (2011, December 15). New Java 7 Features: Using String in the Switch Statement Tutorial. The Server Side. Retrieved from https://www.theserverside.com/tutorial/The-Switch-to-Java-7-Whats-New-with-Conditional-Switches

Published by
Essays
View all posts