You can easily check the local type inference result by var
introduced in Java 10 with JShell. [^ 1]
[^ 1]: / vars
is a JShell command.
JShell also displays the inference results of the Generics Type Parameters section, so it may be more useful than the Java compiler (javac
). (* There are individual differences)
$ jshell
| Welcome to JShell -- Version 10
| For an introduction type: /help intro
jshell> var as = new ArrayList<String>();
as ==> []
jshell> var ao = new ArrayList<>();
ao ==> []
jshell> var ar = new ArrayList();
ar ==> []
jshell> /vars
| ArrayList<String> as = []
| ArrayList<Object> ao = []
| ArrayList ar = []
jshell> var li = List.of(1, 2);
li ==> [1, 2]
jshell> var lx = List.of(1, 2.0);
lx ==> [1, 2.0]
jshell> var ly = List.of(1, "2");
ly ==> [1, 2]
jshell> /vars
| List<Integer> li = [1, 2]
| List<Number&Comparable<? extends Number&Comparable<?>>> lx = [1, 2.0]
| List<Serializable&Comparable<? extends Serializable&Comparable<?>>> ly = [1, 2]
For complex types inferred by the variables lx
, ly
, see Type inference and infinite type. thing.
Recommended Posts