Java 7 was released around the mid of 2011 and has some new language
features that may be have been overlooked by Java developers.
In this article, we will explore some of those capabilities through
code samples.
So without further ado, lets get started !!!
Java 7 :: New Features
Binary Literals
Integral types byte, short,
int, and long can
also be expressed using the binary number system (0s
and 1s). To specify a binary value, prefix
the value with a 0b or 0B.
The following is the example that demonstrates the use of binary
literals:
Executing the program will generate the following output:
Output.1
a [11111111111111111111111110101010] = -86, b [11110000] = 240, c [10011001] = 153, d [101110111011] = 3003
Underscore Literals
To improve readability of numeric literals in the code, one can use
underscores between digits to separate a group of digits.
The following is the example that demonstrates the use of underscore
literals:
Executing the program will generate the following output:
Output.2
a = -91, b = 1234, c = 12345, d = 123456, e = 1.23450
Diamond Operator
One can minimize extra typing of the type arguments (that was required
in the earlier versions of Java) to invoke the constructor of a generic
class with an empty type parameter <>.
The Java the compiler will infer the type arguments from the context.
The following is the example that demonstrates the use of the diamond
operator:
Executing the program will generate the following output:
Output.3
list = [One, Two, Three]
Strings with Switch
The switch statement now supports the use of
Java String object in its expression. The switch statement compares the String
object from its expression with each of the case
values using the String.equals() method which
is case sensitive.
The following is the example that demonstrates the use of Java String
in the switch statement:
Executing the program will generate the following output:
Output.4
TWO
Try with Resources
In the earlier versions of Java, if we had to work with file, network
or database resources, we would open the resource within the try
block, handle any exceptions in the catch
block and close the resource in the finally
block.
The following is the example that demonstrates the opening and reading
a file:
Executing the program will generate the following output:
With the try-with-resources statement, the try
statement declares one or more resource(s). The try-with-resources
statement ensures that each of the resource(s) is closed at the end of
the statement.
The following is the example that demonstrates the opening and reading
a file using the try-with-resources statement, resulting in elegant
code:
Executing the program will generate the same result as in Output.5
above.
Catching Multiple Exception Types
In the earlier versions of Java, we would have multiple catch
blocks to handle the different types of exceptions emitted from the try block.
The following is the example that demonstrates the handling of multiple
exception types:
Executing the program will generate the following output:
Output.6
->String: 123<-
->Number: 345<-
In Java 7, a single catch statement can
handle the different exception types.
The following is the example that demonstrates a single catch
statement handling different exception types, resulting in elegant code:
Executing the program will generate the same result as in Output.6
above.