📜 ⬆️ ⬇️

What will happen in Java 7 - the final list

Joe Darcy (leader of the Project Coin project from Sun) posted a final list of Java 7 language innovations (the original is here ). These innovations are:


String in switch-case expressions:
Example:

  String s = ... 
 switch (s) { 
     case "foo": processFoo (s); 
     break; 
 } 

')
Automatic resource management
Currently, in order to free up resources, in addition to memory, one has to use the try-finally construct. The new approach will make the code more compact and robust to errors. For example, instead of this code:

  BufferedReader br = new BufferedReader (new FileReader (path)); 
 try {
     return br.readLine (); 
 } 
 finally { 
     br.close (); 
 } 

you can write:

  try (BufferedReader br = new BufferedReader (new FileReader (path)) {
     return br.readLine (); 
 } 


Improved type inference when creating generic instances

Today in Java it is often necessary to repeat when initializing a variable with a generic type. Java 7 will do the same thing more elegantly. For example, the old construction:

  Map <String, List <String >> anagrams = new HashMap <String, List <String >> (); 

can be rewritten as:

  Map <String, List <String >> anagrams = new HashMap <> (); 


Improved method call with variable number of arguments (varargs)

Now the warning will be issued not at the point of the method call, but at the place of its declaration.
Earlier:

  static <T> List <T> asList (T ... elements) {...} 
 static List <Callable <String >> stringFactories () { 
     Callable <String> a, b, c;  ... 
     * // Warning: ** "uses unchecked or unsafe operations" *
     return asList (a, b, c); 
 } 

Now:

  * // Warning: ** "enables unsafe generic array creation" * 
 static <T> List <T> asList (T ... elements) {...} 
 static List <Callable <String >> stringFactories () {
     Callable <String> a, b, c;  ... 
     return asList (a, b, c); 
 } 


Improved Literals

There will be two innovations: binary literals (of the form 0b101 or 0B101) and support for underscores in numeric literals for better readability (for example, 9_223_372_036_854_775_807L).

Built-in language collection support

Combines two innovations: collection literals and a new type of access to Lists and Maps (via []). Collection literals simplify the initialization of new Lists, Sets and Maps. Earlier:

  final List <Integer> piDigits = Collections.unmodifiableList (Arrays.asList (3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 9)); 


Now:

  final List <Integer> piDigits = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 9]; 


When using curly braces, a Set is created:

  final Set <Integer> primes = {2, 7, 31, 127, 8191, 131071, 524287}; 


Old way to initialize Map:

  final Map <Integer, String> platonicSolids;
 static { 
     solids = new LinkedHashMap;
     solids.put (4, "tetrahedron"); 
     solids.put (6, "cube"); 
     solids.put (8, "octahedron"); 
     solids.put (12, "dodecahedron"); 
     solids.put (20, "icosahedron"); 
     platonicSolids = Collections.immutableMap (solids); 
 } 

New way to initialize Map:

  final Map <Integer, String> platonicSolids = {4: "tetrahedron", 6: "cube", 8: "octahedron", 12: "dodecahedron", 20: "icosahedron"}; 


Note that collections created this way will be immutable.

JSR 292 support (dynamic language typing)

To support dynamic typing, a new type of java.dyn.Dynamic is introduced. Example of use:

  Dynamic x = (any type of expression can go here); 
 Object y = x.foo ("ABC"). Bar (42) .baz (); 

This code will always be compiled, but it will produce a run-time exception if the specified methods are missing in a variable of type Dynamic.

Rejected offers

Some potential innovations were canceled at the very end of the discussion. Among them: improved exception handling, Elvis operator and other null-safe operators, as well as large arrays.

So what do you think about this?

Update: I apologize for the jambs in the original version. Preview for some reason published a topic. Yes, and Habr-tags about generics stumbled.
Update 2: antalus rightly noted that this is not all innovations in Java 7, but only related to the language. Full list here .

Source: https://habr.com/ru/post/69811/


All Articles