var user=new ArrayList<User>();
java10之前的变量声明:
URL codefx = new URL("http://codefx.org") URLConnection connection = codefx.openConnection(); Reader reader = new BufferedReader( new InputStreamReader(connection.getInputStream()));
java10
var codefx = new URL("http://codefx.org"); var connection = codefx.openConnection(); var reader = new BufferedReader( new InputStreamReader(connection.getInputStream()));
var foo; foo = "Foo";
var ints = {0, 1, 2};
var appendSpace = a -> a + " ";
private var getFoo(){}
除了局部变量,for循环是唯一可以使用 var的地方:
var numbers = List.of("a", "b", "c"); for (var nr : numbers) System.out.print(nr + " "); for (var i = 0; i < numbers.size(); i++) System.out.print(numbers.get(i) + " ");