Article

Detail Description about Java 7 Features

Topic: Coach Training and CertificationPublished December 22, 2017
No ratings yet561 viewsSign in to rate
Java switch case StringrnBeing 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.rnJava Switch CasernJava 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.rnJava switch case String ExamplernHere 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.rnKeys points to know for java switch case String are:rn1. Java switch case String make code more readable by removing the multiple if-else-if chained conditions.rn2. Java switch case String is case sensitive, the output of example confirms it.rn3. 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.rn4. 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.rn5. 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.rnJava Try with ResourcesrnOne of the Java 7 feature is try-with-resources statement for automatic resource management.rnA 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.rnJava try with resources benefitsrnSome 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.rn1. 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.rnJava 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.rnTry with Resources ExceptionsrnThere is one difference to note between try-catch-finally and try-with-resources in case of exceptions.rnIf an exception is thrown in both try block and finally block, the method returns the exception thrown in finally block.rnFor 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.rnTo better clarify this difference, we will see a sample code.rnpackage 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 { @Overridern public void close() throws Exception { System.out.println("Closing MyResource"); throw new Exception("Exception in Closing"); } } }rnOutput of the above program is:rnMyResource created in try-with-resourcesrnClosing MyResourcernException in tryrnMyResource created in try blockrnClosing MyResourcernException in Closing Binary Literals in Java – Java 7 FeaturernBinary 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.rnThis 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.

Further reading

Further Reading

4 total

Article

How Coaching Turns Knowledge Into Change Walk into any bookstore or scroll through your favorite podcast feed, and you’ll see the same thing: endless tips, strategies, and “life hacks” promising transformation.rnWe devour them. We highlight paragraphs. We even try to practice what we’ve learned. But somehow, the big shifts never stick. Weeks later, the old patterns creep back in.rnSo why does it happen? Why do so many smart, motivated people keep getting stuck — eve

October 6, 2025

Article

grinding and cutting a variety of materials across numerous industries. Understanding their construction, types and safety precautions is mandatory for anyone working with these powerful tools. This article will explore the basics of abrasive wheels, offering insights into their components, maintenance and legal requirements to ensure both effective and safe usage. Exploring the Basics of Abrasive Wheels Abrasive wheels are critical tools in various industrial applications, f

March 6, 2025

Article

The internet we use daily, known as the surface web, represents only a fraction of the entire digital landscape. Beneath this visible layer lies the deep web and the dark web, where anonymity, privacy, and unrestricted information exchange thrive. Among the most well-known directories for accessing dark web content is the Uncensored Hidden Wiki . This article delves into the history, significance, risks, and access methods of the Uncensored Hidden Wiki, providing a comprehens

January 31, 2025

Article

If you're searching for exceptional piano classes near you, there are several excellent options in Toronto to consider. For those eager to learn the piano, finding a school or instructor that offers expert guidance, comprehensive lesson plans, and a supportive environment is key. Catering to both beginners and advanced players, many schools in the Toronto area provide tailored lessons to help students achieve their musical aspirations. What to Look for in Piano Lessons When

January 1, 2025