There are a number of features in Java 7.
Some of them are listed below-
- diamond operator
- strings in switch statements,
- multi-catch exception handling,
1. Diamond operator
// Old way-
// Old way-
Map<String, List<Moments>> momentsList = new HashMap<String, List<Moments>>();
//New way-
Map<String, List<Moments>> momentsList = new HashMap<>();
2. Strings in switch statements
// Old way-
private void Show(Profile p) {
String status = p.getStatus();
if (status.equalsIgnoreCase(NEW)) {
newProfile(p);
} else if (status.equalsIgnoreCase(OLD)) {
validateProfile(p);
} else if (status.equalsIgnoreCase(PENDING)) {
pendingProfile(p);
}
}
// New Way-
private void Show(Profile p){
String status = p.getStatus();
switch(status) {
caseNEW:
newProfile(p);
break;
caseOLD:
validateProfile(p);
break;
casePENDING:
pendingProfile(p);
break;
default:
break;
}
}
3. Exception handling
// Old-
public void oldMultiCatch() {
try {
methodThrowsThreeExceptions();
} catch (Exception1 e) {
// e.getMessage();
} catch (Exception2 e) {
// e.getMessage();
} catch (Exception3 e) {
// e.getMessage();
}
}
// New-
public void newMultiCatch() {
try {
methodThrowsThreeExceptions();
} catch (Exception1 | Exception2 | Exception3 e) {
// log all Exceptions
// e.getMessage();
}
}
4. multi multi-catch
public void multiMultiCatch() {
try{
methodThrowsThreeExceptions();
} catch(Exception1 e) {
// e.getMessage();
} catch(Exception2 | Exception3 e) {
// log all Exceptions
//e.getMessage();
}
}
No comments:
Post a Comment