2. 2Zooz Proprietary and Confidential
Strings in Switch statements
public String getTypeOfDayWithSwitchStatement(String dayOfWeekArg) {
String typeOfDay;
switch (dayOfWeekArg) {
case "Monday":
typeOfDay = "Start of work week";
break;
case "Tuesday":
case "Wednesday":
case "Thursday":
typeOfDay = "Midweek";
break;
case "Friday":
typeOfDay = "End of work week";
break;
case "Saturday":
case "Sunday":
typeOfDay = "Weekend";
break;
default:
throw new IllegalArgumentException("Invalid day of the week: " + dayOfWeekArg);
}
return typeOfDay;
3. 3Zooz Proprietary and Confidential
Type Inference for Generic Instance Creation
For example, consider the following variable declaration:
Map<String, List<String>> myMap = new HashMap<String, List<String>>();
In Java SE 7, you can substitute the parameterized type of the constructor with an empty set of type parameters (<>):
Map<String, List<String>> myMap = new HashMap<>();
See also: http://docs.oracle.com/javase/7/docs/technotes/guides/language/type-inference-generic-instance-creation.html
4. 4Zooz Proprietary and Confidential
The try-with-resources Statement
Prior to Java SE 7, you can use a finally block to ensure that a resource is closed regardless of whether the try statement
completes normally or abruptly. The following example uses a finally block instead of a try-with-resources statement:
static String readFirstLineFromFileWithFinallyBlock(String path)
throws IOException {
BufferedReader br = new BufferedReader(new FileReader(path));
try {
return br.readLine();
} finally {
if (br != null) br.close();
}
}
In java 7:
static String readFirstLineFromFile(String path) throws IOException {
try (BufferedReader br =
new BufferedReader(new FileReader(path))) {
return br.readLine();
}
}
5. 5Zooz Proprietary and Confidential
The try-with-resources Statement - continue
• Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be
used as a resource.
public interface Closeable {
void close() throws java.io.IOException;
}
• You may declare one or more resources in a try-with-resources statement
• Note: A try-with-resources statement can have catch and finally blocks just like an ordinary try statement. In a try-with-
resources statement, any catch or finally block is run after the resources declared have been closed.
• You can retrieve suppressed exceptions by calling the Throwable.getSuppressed method from the exception thrown by the
try block.
See also: http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
7. 7Zooz Proprietary and Confidential
Catching Multiple Exception Types and Rethrowing Exceptions with
Improved Type Checking – continue
Rethrowing Exceptions with More Inclusive Type Checking
Java 6:
public void rethrowException(String exceptionName) throws Exception {
try {
if (exceptionName.equals("First")) {
throw new FirstException();
} else {
throw new SecondException();
}
} catch (Exception e) {
throw e;
}
}
Java 7:
public void rethrowException(String exceptionName)
throws FirstException, SecondException {…
}
In releases prior to Java SE 7, you cannot do so. Because the exception parameter of the catch clause, e, is type Exception, and
the catch block rethrows the exception parameter e, you can only specify the exception type Exception in the throws clause of
the rethrowException method declaration.
See also: https://docs.oracle.com/javase/8/docs/technotes/guides/language/catch-multiple.html
8. 8Zooz Proprietary and Confidential
Java 8
http://www.slideshare.net/victorperepelitsky/fun-with-java-8
Streams Versus Collections
Both the existing Java notion of collections and the new notion of streams provide interfaces to a sequence of elements. So
what’s the difference? In a nutshell, collections are about data and streams are about computations.
http://www.oracle.com/technetwork/articles/java/ma14-java-se-8-streams-2177646.html