Java 7 Features that I have missed
I never have a chance to use the new features of Java 7 in my day-to-day basis project, so just want to keep it as a reference here:
- Strings in switch
- Diamond Operator
- try-with-resources statement
- Multi-catch exception
- Files
- equals, hashCode and comparaTo methods
- Null checks
Strings in switch
String s = ...
switch (s) {
case "hello": ...; break;
...
}
Diamond Operator
Instread of:
Map<String, Integer> = new HashMap<String, Integer>();
You can write:
Map<String, Integer> = new HashMap<>();
try-with-resources statement
try (Resource res = ...) {
...
}
when the try
block exit, res.close()
will be called autometically.
Multi-catch exception
try {
//do someting;
}
catch(Exception1, Exception2 e) {
handleException(e)
}
Files
It provided static methods to manage a file e.g. Files
, Paths
:
Path absolute = Paths.get("/", "home", "dev")
Path relative = Paths.get("app", "config", "user.properties")
equals, hashCode and comparaTo methods
There are static mehtods of Objects
class:
Objects.equals(a, b)
Objects.hashCode(a)
Objects.hash(first, last)
Integer.compare(a, b)
Null checks
A static method to check null:
String hello = ...
Objects.requireNonNull(hello)