Java 8 - Part I [Language Enhancements]
Hello, everybody!!!
This post starts a series about the changes that you can find between version 6 to 8. For now, let’s see some enhancing of the Java Language.
Summary
Switch statement
Before the Java 7, the switch statement allows you use only primitives expressions (int, short, byte, char) and enumerated types. A float or double will generate a compile error. Since Java 7, Strings are also allowed. Now, you can use the equivalent primitive numeric wrapper classes: Character, Byte, Short and Integer.
You should have in your mind that some scenarios can come up a compile error. For example, each case needs either a constant value or a final variable initialized at declaration, and each case needs to be unique (different values).
Literals and Underscore Characters
Numeric literals can be byte, short, int, long, float or double, generally expressed in decimal base. You can also express that using:
- Octal: it uses number '0'as prefix (Ex. 017)
- Hexadecimal: it uses number '0' followed by character 'x' as prefix (Ex. 0x2B)
- Binary: it uses number '0'followed by 'b'or 'B' as prefix (Ex. 0b0010110)
Since version 7, you can use underscore to make it better to read. However, you cannot add underscores at the beginning of a literal, the end of a literal, right before a decimal point, or right after a decimal point.
Using try-with-resources statements
The try-with-resource block comes out in Java 7. This block permit declares resource that can be closed without doing it explicitly in a finally block, and that resources exist only in try block scope. In this feature, Java is responsible for close the resource. That structure has a finally block implicitly. However, you can have catch and/ or finally blocks. The implicit finally block runs before any programmer-coded ones. If more than one resource is declared, Java closes resources in the reverse order from which it created them.
The resource in try block has to implement java.lang.AutoCloseable and his close() method. That interface was introduced in Java 7 and one difference between AutoCloseable and Closable is that the second one restricts the type of exception thrown to IOException.
If the method in the try block throws an exception, the java uses the suppressed exception idea, or accumulate the exception. So, all but the first exceptions are suppressed. However, if the exception is thrown in finally block the previous exception is lost.
Multiple Exception types
In previous versions, the catch block was used to handle only one exception (checked or unchecked). If exist more than one block, it's necessary to respect the hierarchy of the classes, or a compile-time error is generated.
Since Java 7, catch block is optional. However, if you don't have a catch block, you have to have a finally block.
It's also available the use of multi-catch. You are able to declare multiple exceptions in the same catch. In this new structure, you have to know that the variable is final, it must appear only once and at the end, and you cannot combine subclasses and their superclasses in the same catch block.
Default methods of an interface
An interface is a contract with just methods definitions, that have to be implemented in the concrete class that implement it. If the interface implements some methods it has to be declared as abstract.
An interface cannot be final and the methods are public and abstract. The fields declared in an interface are constants and by default, they are public, static and final.
Using this structure, if you add a new method definition in the interface, the classes that implement the interface will have a compile error. To fix this, it's necessary to update these classes. It'll be a problem if you have many classes, or these classes are not available or even the new method not make sense for some class.
The Java 8 add the default method, a method with a body and that is not an abstract method. It can be optionally overridden on the concrete class. In the same way, you can also implement a static method. Now, the interface can provide behaviour. It's possible implements many default methods, and each one needs to have a body.
If you implement two interfaces with the same default method, a compile error will happen.
- Default methods cannot be final.
- Default methods cannot be synchronized.
- Default methods are always public.
- You cannot have default methods for the Object's class methods.
Use static methods of an interface
Static methods are utility methods created to assist default methods. Static methods on interfaces are not inherited.
Related Posts
- Java 8 - Part VII [Collections]
- Java 8 – Part VI [File IO NIO.2]
- Java 8 – Part V [Concurreny]
- Java 8 – Part IV [Streams]
- Java 8 – Parte III [Lambda]
- Java 8 - Part II [Localization, Date, Time]
- Java 8 - Language Enhancements
- JVM