Generics

Generics Java Generics features were introduced from Java Se 5 onwards. Before Java 5, we can store any type of objects in collection i.e. non generic. With inclusion of Generics we need to store only specific type of objects only. Let’s look at the benefits of generics Compile time checking: Generics allows you to check…

Continue reading

Java 7 new features

Java 7 new Features Some of the important Java 7 features are … String in Switch Statement: Before Java se 7, int or char only allowed for switch case, now in JDK 7 we can use String in switch case statement. String phone = “work”; switch (phone) { case”mobile”:System.out.println(“User selected mobile number”);break; case”work”:System.out.println(“User selected landline…

Continue reading

Catching Multiple Exceptions

Catching Multiple Exceptions Before Java 7, to handle multiple Exceptions need to have multiple catch blocks even though all will perform similar tasks which will cause code duplication. Let’s see example how multiple exceptions handled before Java se 7 public static void testmultiCatch() { String line; try(FileReader f = new FileReader(“test.txt”); BufferedReader br = new BufferedReader(f);) …

Continue reading

How to Use Underscores in Numerical literals

 Underscores in Numerical literals Underscores in Numerical literals feature in Java se 7 will improve code readability and code quality like String in Switch case statement. When you are dealing with large numbers it is hard to figure out just by looking that number. For example 100000000, it’s hard to tell that number immediately it…

Continue reading

How to use try with resources

Try with resources  Before Java se 7, as part of coding best practice we will close all our resources in finally block regardless of whether try block completes normally or abruptly. To explain this see the below example public static void testTryResources() { FileReader f = null;BufferedReader br = null; try { f = new…

Continue reading

String in Switch Statement with example

String in Switch Statement with example Before JDK 7, int or char only allowed for switch case, now in JDK 7 we can use String in switch case statement. How this will be helpful in programmer’s perspective? Let’s see with a simple example. Using String in switch case Statement public static void phoneOptions(String phone) {…

Continue reading