Java switch case String
Being a java programmer, I know the importance of String and how many times it’s used for conditional flow. Whether you have a simple method that behaves differently for different input String or a Servlet controller class to check the incoming action and process it accordingly, we use String and compare it to determine the flow.
Java Switch Case
Java switch case is a neat way to code for conditional flow, just like if-else conditions. Before Java 7, the only means to achieve string based conditional flow was using if-else conditions. But Java 7 has improved the switch case to support String also.
Java switch case String Example
Here I am providing a java program that shows the use of String in java switch case statements. For comparison, I am also providing another method which does the same conditional flow using if-else conditions.
Keys points to know for java switch case String are:
1. Java switch case String make code more readable by removing the multiple if-else-if chained conditions.
2. Java switch case String is case sensitive, the output of example confirms it.
3. Java Switch case uses String.equals() method to compare the passed value with case values, so make sure to add a NULL check to avoid NullPointerException.
4. According to Java 7 documentation for Strings in Switch, java compiler generates more efficient byte code for String in Switch statement than chained if-else-if statements.
5. Make sure to use java switch case String only when you know that it will be used with Java 7 else it will throw Exception.
Java Try with Resources
One of the Java 7 feature is try-with-resources statement for automatic resource management.
A resource is an object that must be closed once your program is done using it. For example a File resource or JDBC resource for database connection or a Socket connection resource. Before Java 7, there was no auto resource management and we should explicitly close the resource once our work is done with it. Usually, it was done in the finally block of a try-catch statement. This approach used to cause memory leaks and performance hit when we forgot to close the resource.
Java try with resources benefits
Some of the benefits of using try with resources in java are;
• More readable code and easy to write.
• Automatic resource management.
• Number of lines of code is reduced.
• No need of finally block just to close the resources.
• We can open multiple resources in try-with-resources statement separated by a semicolon.
1. When multiple resources are opened in try-with-resources, it closes them in the reverse order to avoid any dependency issue. You can extend my resource program to prove that.
Java 7 has introduced a new interface java.lang.AutoCloseable. To use any resource in try-with-resources, it must implement AutoCloseable interface else java compiler will throw compilation error.
Try with Resources Exceptions
There is one difference to note between try-catch-finally and try-with-resources in case of exceptions.
If an exception is thrown in both try block and finally block, the method returns the exception thrown in finally block.
For try-with-resources, if exception is thrown in try block and in try-with-resources statement, then method returns the exception thrown in try block.
To better clarify this difference, we will see a sample code.
package com.journaldev.util;

public class Java7ResourceManagement {

public static void main(String[] args) throws Exception {
try {
tryWithResourceException();
} catch (Exception e) {
System.out.println(e.getMessage());
}
try {
normalTryException();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}

private static void normalTryException() throws Exception {
MyResource mr = null;
try {
mr = new MyResource();
System.out.println("MyResource created in try block");
if (true)
throw new Exception("Exception in try");
} finally {
if (mr != null)
mr.close();
}

}

private static void tryWithResourceException() throws Exception {
try (MyResource mr = new MyResource()) {
System.out.println("MyResource created in try-with-resources");
if (true)
throw new Exception("Exception in try");
}
}

static class MyResource implements AutoCloseable {

@Override
public void close() throws Exception {
System.out.println("Closing MyResource");
throw new Exception("Exception in Closing");
}

}
}
Output of the above program is:
MyResource created in try-with-resources
Closing MyResource
Exception in try
MyResource created in try block
Closing MyResource
Exception in Closing

Binary Literals in Java – Java 7 Feature
Binary literals are new features in Java 7. As you all know that we can write integral types (byte, short, int, and long) in Binary and Hexadecimal formats but from Java 7 onwards we can write these numbers in binary format also. The number should be prefixed with 0b or 0B to be treated as binary literal.
This feature is very helpful to bit-oriented systems like processors, network protocols and bitmapped hardware device. Early the programmer used to transform from binary to decimal/hexadecimal and vice versa. Using this feature will remove this transformation and chances of error will be less in this conversion.

Author's Bio: 

Infocampus is a best java training institute locating in Marathahalli, Bangalore. It is an excellent institute for java/j2ee classes bangalore. 100% Practical Oriented classes will be conducted by 6+ years experience professional Trainers. Visit http://infocampus.co.in/java-training-bangalore.html to get much information on java course. Contact 9738001024 and attend free demo