Java 7 Type Inference for Generic Instance Creation PRO

As of Java 7 we can take out the type arguments required to instantiate a generic class as long as the compiler can infer the type arguments from the context. Many developers name the remaining pair of angle brackets as ‘diamond’. package com.abelski.samples; import java.util.LinkedList; import java.util.List; public class GenericsTypeInference { public static void main(String[] […]

Java 7 Multiple Exception Types Catch PRO

As with Java 7 we can handle more than one type of exception using a single catch block. We should use the ‘|’ operator for putting them together in the same catch statement. When catch handles more than one type of exception the catch parameter is implicitly final. We won’t be able to assign it with […]

The Java 7 try-with-resources Statement PRO

As of Java 7 we can code a try statement that declares one or more resources. Each resource is an object that must be closed after the program is finished. The try-with-resources statement ensures that each resource is closed at the end of the statement. Each object considered as a resource must be instantiated from a class […]

Java 7 Binary Number System PRO

Java 7 allows us to express integral numbers using the binary numbers system. We just need to prefix our integral number with either ‘ob’ or ‘0B’. package com.abelski.samples; public class BinaryDemo { public static void main(String[] args) { int a = 0b101; int b = 0b011; int c = a | b; System.out.println(c); } } […]

Java 7 Switch Case PRO

As of Java 7 we can write a switch case statement that works on strings. This new capability might assist us with writing a shorter code. package com.abelski.samples; public class Demo { public static void main(String[] args) { String operator = args[1]; double numA = Double.parseDouble(args[0]); double numB = Double.parseDouble(args[2]); String result; switch(operator) { case […]

Java 7 Underscores in Numeric Literals PRO

As of Java 7, we can improve the readability of our code by adding underscores in between digits in numerical literals. package com.abelski.samples; public class SeparatorClass { public static void main(String[] args) { double num = 1_424_234.532; System.out.println(num); } } The following video clips overviews this code sample and shows its execution.

Skip to content Update cookies preferences