Try-With-Resources
Java 7 introduced the
try-with-resources statement, which allowed resources that
implement the AutoCloseable interface to be declared as parameters inside
of the try block. When the try block completes,
the JVM will automatically
call the close() method on those resources, eliminating the need for
a finally block.
Generics
Compile-Time Errors vs Runtime Errors
Compile-time errors are errors that prevent
a program from compiling, such as
syntax errors. Runtime errors are errors that occur during the execution of an
application, such as casting an object to an invalid type. Compile-time errors are
easier to fix because the compiler tells you where they occur.
Runtime errors are
more difficult to detect and may cause unpredictable behavior in unrelated parts
of the application. It is therefore preferable to catch as many errors as possible
during the compile-time phase.
Generics
Generics were introduced to prevent runtime errors caused by invalid type
casting. For example, it would be legal to add a
String to a non-generic List
and retrieve it with a cast to an Integer, resulting
in a ClassCastException. However the same situation with a generic
List
would cause a compile-time error. Without generics, the only
way to accomplish type safety would require a List implementation for every
possible object, such as a StringList, IntegerList, etc.