0% found this document useful (0 votes)
562 views

Project Coin Features in Java 7

Java 7 introduces several new features including underscores in numeric literals to improve readability, binary literals, using strings in switch statements, multi-catch exception handling, improved type inference for constructors, immutable collections, index access for lists and maps, and new utility methods in java.util.Objects. A sneak peek at Java 7 was provided with details on each new feature.

Uploaded by

Abhishek Asthana
Copyright
© Public Domain
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
562 views

Project Coin Features in Java 7

Java 7 introduces several new features including underscores in numeric literals to improve readability, binary literals, using strings in switch statements, multi-catch exception handling, improved type inference for constructors, immutable collections, index access for lists and maps, and new utility methods in java.util.Objects. A sneak peek at Java 7 was provided with details on each new feature.

Uploaded by

Abhishek Asthana
Copyright
© Public Domain
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

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 = Arrays.asList(“a", “b"); //
inference
List<String> list2 = Collections.emptyList(); //
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 = Collections.unmodifiableList(
Arrays.asList(“a”, “b” , “c”)
);

HashMap<String, Double> mMap =


new HashMap<String, Double> ();
mMap.put(“a”,1);
mMap.put(“b”,2);

final Map<String,Double> uMap =


Collections.unmodifiableMap(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 = Arrays.asList(“a”,“b”,“c”);
Now: uList.get(0);
in Java 7: uList[0];

final Map<String,String> uMap =


{“a” : “A” , “b” : “B” , “c” : “C”};
Now: uMap.get(“a”);
in Java 7: uMap[“a”];
java.util.Objects
<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
o.hashCode()
java.util.Objects (contd.)
boolean equals(Object a, Object b) : Return true if the two
arguments are null or they are both not null and a.equals(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 Arrays.deepEquals 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
c.compare(a, b).
Java 7
Java 7 scheduled to be released in September 2010.

Snapshot releases available:


http://dlc.sun.com.edgesuite.net/jdk7/binaries/index.ht
ml

Only IDE supporting Java 7 now is Netbeans 6.9

You might also like