Java 7 (Dolphin)
A sneak peek at some new features
(Project Coin)
Abhishek Asthana
Underscores in numbers
To improve readability.
int newInt = 55_12;
float newFloat = 1234_3456.36f;
long newHex = 0x123A_34BL;
int newOct = 023_42;
New Binary literal
Binary joins the Decimal, Octal, Hexadecimal club
int hex = 0x56B;
int oct = 0677;
int binary = 0b11001;
Strings in switch
Not only integers, now Strings can also be used as key
for switch. (with strings attached of course!)
private static final String constant = “constant";
private void stringSwitch() {
final String finalVariable = “finalVariable";
String key = "key";
switch (key) {
case “something": {break;}
case finalVariable:
case constant: {break;}
default: break;
}
}
Multi catch
try {
} catch (ExceptionA a) {
// log and rethrow exception
} catch (ExceptionB b) {
// log and rethrow exception
} catch (ExceptionC c) {
// log and rethrow exception
}
try{
} catch (ExceptionA a|ExceptionB b|ExceptionC c){
// log and rethrow exception
}
Improved Type Inference
List<String> list1 = [Link](“a", “b"); //
inference
List<String> list2 = [Link](); //
inference
List<String> list3 = new ArrayList<String>(); //
no inference
Inference now available for constructors also:
List<String> list3 = new ArrayList<>();
Immutable Collections
final List<String> uList = [Link](
[Link](“a”, “b” , “c”)
);
HashMap<String, Double> mMap =
new HashMap<String, Double> ();
[Link](“a”,1);
[Link](“b”,2);
final Map<String,Double> uMap =
[Link](mMap);
Immutable Collections (contd.)
final List<String> uList = [“a” , ”b” , ”c”];
final Set<String> uSet = {“a” , “b” , “c” };
final Map<String,String> uMap =
{“a” : “A”, “b” : “B” , “c” : “C”};
Index-access for Lists & Maps
ArrayList uList = [Link](“a”,“b”,“c”);
Now: [Link](0);
in Java 7: uList[0];
final Map<String,String> uMap =
{“a” : “A” , “b” : “B” , “c” : “C”};
Now: [Link](“a”);
in Java 7: uMap[“a”];
[Link]
<T> T nonNull(T o) : Return object of a non-null object else throw NPE.
<T> T nonNull(T o, String msg): Return object of a non-null object
else throw NPE with ‘msg’.
String toString(Object o) : Return the toString() value of a non-null
object otherwise “null”.
String toString(Object o, String nullMsg) : Return the
toString() value of a non-null object otherwise return ‘nullMsg’.
int hash(Object… values) : Compute a hash code for all the given
values
int hashCode(Object o) : If o is null return 0 otherwise return
[Link]()
[Link] (contd.)
boolean equals(Object a, Object b) : Return true if the two
arguments are null or they are both not null and [Link](b) return
true, otherwise false.
boolean deepEquals(Object a, Object b) : Almost the
same as the first method except that if both a and b are arrays, the
equality is evaluated using [Link] method.
<T> int compare(T a, T b, Comparator<? super T> c) :
This method returns 0 if a == b or if both are null otherwise
[Link](a, b).
Java 7
Java 7 scheduled to be released in September 2010.
Snapshot releases available:
[Link]
ml
Only IDE supporting Java 7 now is Netbeans 6.9