Skip to content

Instantly share code, notes, and snippets.

@entrofi
Last active April 28, 2021 06:51
Show Gist options
  • Save entrofi/c0354332f46a337c7e343cd92959a6da to your computer and use it in GitHub Desktop.
Save entrofi/c0354332f46a337c7e343cd92959a6da to your computer and use it in GitHub Desktop.
Java 10 Developer Features

Summary and Examples of Java 10 Developer Features

Local variable type inference

var order = new Order(); // order type is inferred
public List<Order> getCustomerOrders(String customerId){...}
var orderList = getCustomerOrders("1234"); //List of orders is inferred
var stringArray = new String[11];
Arrays.fill(new String[11], 0, 10, "String Array");
for(var stringInArray: stringArray) { //stringInArray is inferred from stringArray
System.out.println(stringInArray);
}
public class var {
}
//Build output
java: 'var' not allowed here
as of release 10, 'var' is a restricted type name and cannot be used for type declarations
//var as a method name is allowed
public void var() {
System.out.println("Hello var!");
}
public void someMethod() {
//var as a variable name is allowed
var var = 3;
}
public class AClass {
//var as field name is allowed
public int var;
}
//package name is allowed
package var;
void someMethod() {
Map<String, List<Order>> orderMap = new HashMap<>();
}
//Can now be written as
void someMethod() {
var orderMap = new HashMap<String, List<Order>>();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment