Category: Java 7 features

Java 7: Underscores in numeric literals

The underscores in numeric literals is included in JSE7. You can use the underscore character between digits in a numerical literals similar to a comma or a space as a separater. This feature is used to improve the readability of the code. For more info click here to read Oracle’s explanation. Notice: Underscores have to be located […]

Java 7: Type inference

Type inference is included in JSE7. As long as the compiler can infer the arguments type from the context, you can use an empty set of parameters type <>. We call this pair of angle brackets the diamond. See the example below! For more info click here to read Oracle’s explanation. Example 1: Type inference code What is […]

Java 7: Try with resources

The feature try with resources is included in JSE7. A resource is an object, could be a file or a connection, which must be closed when the program doesn’t use it anymore. By using this feature you ensure to close the resources after using them. I used the path “/home/gardun/Documents/mydoc” because I work on Linux, […]

Java 7: Catching multiple exceptions

In the following example we apply catching multiple exception types, which is a new feature in JSE7, In the earlier Java versions you could catch each exception type in a separate block, but in Java 7 you can catch multiple types of exceptions in one block. For more info click here to read Oracle’s explanation. Example 1: […]

Java 7: Using strings in switch statements

This is a simple feature in JSE7, which allows to pass a String to switch statements. That was not allowed in earlier Java versions. Let’s see how it works! For more info click here to read Oracle’s explanation. Example 1: The seasons in a switch statement What is the output of the following code? Author: […]

Java 7: Binary literals

This feature in JSE7 allows the primitive variables byte, short, int and long to express the binary number system by adding the prefix 0b or 0B to the number as shown in the following example. For more info click here to read Oracle’s explanation. Example 1: Binary literal code What is written to the standard […]