Java 10 var: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(8 intermediate revisions by the same user not shown)
Line 5: Line 5:
=Overview=
=Overview=


The feature behind the Java 10 new keyword '''var''' is called local variable type inference.
The feature behind the Java 10 new keyword '''var''' is called local variable type inference: functionality added to the Java compiler, which is now able to infer the type of a variable from the [[Java_Language#Variable_Initializer|variable initializer]]. This new compiler functionality make possible to replace the type in a local variable declaration with the keyword <tt>var</tt>. The compiler fills in the appropriate type:
 
Local variable type inference is a new functionality added to the Java compiler, which is now able to infer the type of a variable from the [[Java_Language#Variable_Initializer|variable initializer]]. This new compiler functionality make possible to replace the type in a local variable declaration with the keyword <tt>var</tt>. The compiler fills in the appropriate type:


Before <tt>var</tt>:
Before <tt>var</tt>:
<syntaxhighlight lang='java'>
<syntaxhighlight lang='java'>
Map<String, List<String>> catalog = new HashMap<>();
</syntaxhighlight>
</syntaxhighlight>


With <tt>var</tt>:
With <tt>var</tt>:
<syntaxhighlight lang='java'>
<syntaxhighlight lang='java'>
var catalog = new HashMap<String, List<String>>();
</syntaxhighlight>
</syntaxhighlight>
<tt>var</tt> may not be used when there is no [[Java_Language#Variable_Initializer|variable initializer]]. Also, polymorphic assignments cannot be generally made when <tt>var</tt> is used, because the type inference associates the variable with a specific type, and not with any of its super types.
Variable inference cannot be used with fields, or within method signatures, and they cannot be used without explicit initialization - the compiler won't know what type to associate them with. Variable inference cannot be used when the variable is initialized with null, for the same reason.


=Details=
=Details=


The <tt>var</tt> inference algorithm looks only at the expression being assigned to the variable in order to deduce the type.
The <tt>var</tt> inference algorithm looks only at the expression being assigned to the variable in the [[Java_Language#Variable_Initializer|variable initializer]] in order to deduce the type.
 
=Idiosyncrasies=
 
<syntaxhighlight lang='java'>
var a = new ArrayList<>();
</syntaxhighlight>
 
associates the variable with an ArrayList<Object>.


=var and Code Readability=
=var and Code Readability=
Line 26: Line 38:


<syntaxhighlight lang='java'>
<syntaxhighlight lang='java'>
Map<String, List<String>> a = getCatalog();
</syntaxhighlight>
is probably better than:
<syntaxhighlight lang='java'>
var a = getCatalog();
</syntaxhighlight>
</syntaxhighlight>


<tt>var</tt> removes the reader ability to guess about the code's intent sim[ply from the type of the variable.
assuming that <tt>getCatalog()</tt> is defined farther away. <tt>var</tt> removes the reader ability to guess about the code's intent sim[ply from the type of the variable.
 
However, there may be cases when var actually improves readability by eliminating obviously redundant type declaration in a very local context. Logically, this is similar to naming a variable "i" instead of "internalProcessId" in a short loop. This is an example where <tt>var</tt> arguably improves readability.
 
Before <tt>var</tt>:
 
<syntaxhighlight lang='java'>
Map<String, List<String>> catalog = new HashMap<>();


However, there may be cases when var actually improves readability by eliminating obviously redundant type declaration in a very local context. Logically, this is similar to naming a variable "i" instead of "internalProcessId" in a short loop. This is an example where <tt>var</tt> improves readability and it comes with an additional, not immediately obvious, advantage.
for(Map.Entry<String, List<String>> c: catalog.entrySet()) {
  List<String> values = c.getValue():
}
</syntaxhighlight>
 
After <tt>var</tt>:


<syntaxhighlight lang='java'>
<syntaxhighlight lang='java'>
var catalog = new HashMap<String, List<String>();
for(var c: catalog.entrySet()) {
  var values = c.getValue():
}
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 19:46, 25 May 2018

Internal

Overview

The feature behind the Java 10 new keyword var is called local variable type inference: functionality added to the Java compiler, which is now able to infer the type of a variable from the variable initializer. This new compiler functionality make possible to replace the type in a local variable declaration with the keyword var. The compiler fills in the appropriate type:

Before var:

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

With var:

var catalog = new HashMap<String, List<String>>();

var may not be used when there is no variable initializer. Also, polymorphic assignments cannot be generally made when var is used, because the type inference associates the variable with a specific type, and not with any of its super types.

Variable inference cannot be used with fields, or within method signatures, and they cannot be used without explicit initialization - the compiler won't know what type to associate them with. Variable inference cannot be used when the variable is initialized with null, for the same reason.

Details

The var inference algorithm looks only at the expression being assigned to the variable in the variable initializer in order to deduce the type.

Idiosyncrasies

var a = new ArrayList<>();

associates the variable with an ArrayList<Object>.

var and Code Readability

Type inference reduces the amount of time it takes to write Java code, but in some cases does not necessarily improve its readability. Some say that developers spend much more time reading source coding than writing it, so probably the amount of time it takes to write the code should be optimized in favor of readability. This is an example where var does not improve readability:

Map<String, List<String>> a = getCatalog();

is probably better than:

var a = getCatalog();

assuming that getCatalog() is defined farther away. var removes the reader ability to guess about the code's intent sim[ply from the type of the variable.

However, there may be cases when var actually improves readability by eliminating obviously redundant type declaration in a very local context. Logically, this is similar to naming a variable "i" instead of "internalProcessId" in a short loop. This is an example where var arguably improves readability.

Before var:

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

for(Map.Entry<String, List<String>> c: catalog.entrySet()) {
   List<String> values = c.getValue():
}

After var:

var catalog = new HashMap<String, List<String>();

for(var c: catalog.entrySet()) {
   var values = c.getValue():
}