Did you know how new features were added into Java 7? The following are functionalities that can be considered.
Source: [1]
Project Coin is all about the changes in the range from syntactic sugar to new language feature. This is what we're talking about today.Since Java 7 is the first version developed in an open source manner, there is an amount of actions that must be performed for any changes as follows:
- Update the Java Language Specification (JSL)
- Implement a prototype in the source compiler
- Add library support essential for the change
- Write tests and examples
- Update document
- Etc, ...
Wow! It is tough, right? Let's take a look what features Project Coin brought to Java 7.
Strings in switch statement
Question: Why do we need "switch"? (already supported by prior Java 7)Answer: To avoid lots of ugly nested "ifs".
Question: Why do we need "string" in "switch"?
Answer: Just to make life in Java 7 a little bit easier. Agree?
Enhanced syntax for numeric literals
Underscores in numbers to improve readability.E.g: 100_000_000 and 10_000_000 are much more readable than 100000000 and 10000000.
Improved exception handling
- Multicatch: it groups a family of exceptions into one catch statement; this feature can reduce code duplication.- Final rethrow: it won't force to declare the exception signature. For example, my IDE complains of the block below with Java 6 compiler, but Java 7.
However, the issue here is the real dynamic type of the exception has been swallowed if we declare the exception signature.
And, the change in Java 7 is just simple as follows:
try {
doSomethingWhichMightThrowIOException();
doSomethingElseWhichMightThrowSQLException();
} catch (final Exception e) {
//...
throw e;
}
Why is final keyword useful? Because it provides a new semantics of catch and rethrow. The appearance of the final keyword indicates that the type that’s actually thrown is the runtime type of the exception that was encountered.
Note: If a catch block handles more than one exception type, then the catch parameter is implicitly final.
Try-with-resources
#Funfact: Virtually no one gets manual resource closing 100 percent right.Therefore, using try-with-resources to eliminate unnecessary bugs from your codebase.
try(OutputStream out = new FileOutputStream(file);
InputStream is = url.openStream()){
byte[] buf = new byte[4096];
int len;
while ((len = is.read(buf)) > 0) {
out.write(buf, 0, len);
}
}
Diamond syntax
To avoid the definition and setup of instances can be really verbose.#Funfact: Why "diamond syntax" name? Because original name, ITIGIC (Improved Type Inference for Generic Instance Creation), is a very long(stupid) name and the shortened type information looks like a diamond.
Simplified varagrs method invocation
What is it? It just moves a warning about type information for a very specific case where varargs combines with generics in a method signature.An admitted weaknesses of Java’s generics—you aren’t normally allowed to create an array of a known generic type. For example, this won't compile:
HashMap<String, String>[] arrayHm = new HashMap<>[2];
So when it runs into this circumstance, the compiler cheats and breaks its own rule about the forbidden array of generic type. It throws the warning "unchecked" which is darkly meaning. Oops! Then, dummy developers want to know why their methods are "unchecked"? They need an explanation.
To make things easier for developers and by annotating the method with "@SafeVarargs", the developer essentially asserts that method doesn't perform any unsafe operations. Known issue! ;)
Reference:
[1]. Benjamin J. Evans and Martijn Verburg, “The Well-Grounded Java Developer: Vital techniques of Java 7 and polyglot programming"