Java Magazine, May/June 2018
ORACLE COM JAVAMAGAZINE MAY JUNE 2018 72 java 10 codes intent simply from the type of the variable there is more of a burden on you as the developer to provide good names for local variables In theory thats something Java developers should already be doing In practice though many readability problems in Java code arent related to new language features but rather to existing practices such as variable naming Type Inference and IDEs One commonly used feature that many IDEs provide is the ability to extract a local variable and in doing so they will infer the correct type of that variable and write it out for you That feature has some overlap with the var keyword in Java 10 Both the IDE feature and var remove the need to write out the type explicitly but they otherwise have different trade ofs The extract local feature generates a local variable with the full In Java 11 to be released a mere six months after Java 10 the var keyword will be allowed within the parameters of a lambda expression explicit type written out in your code whereas var removes the need to have the explicit type written out in your code So although they both have similar value in terms of simplifying the writing of code var alters readability in a way that the extract local feature does not As mentioned before var is mostly a readability benefit but sometimes it can be a hindrance Java Compared to Other Programming Languages Java isnt the only or even the first language to include type inference for variables In fact the type inference introduced in Java 10 with var is a very limited and restricted form of type inference It keeps the approach simple and also ensures that compiler errors related to var declarations are restricted to a single statement because the var inference algorithm looks only at the expression being assigned to the variable in order to deduce the type
You must have JavaScript enabled to view digital editions.