Detail Description about Java 7 FeaturesPosted by Infocampus HR on December 22nd, 2017 Java Switch CaseJava 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 ExampleHere 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:
Java Try with ResourcesOne 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 benefitsSome of the benefits of using try with resources in java are;
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 ExceptionsThere 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 FeatureBinary 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. Like it? Share it!More by this author |