We, Java programmers tend to adopt new featured pretty slow. Because we adore stability and robustness. When a new version of Java is released, it’s much very stable and with a big change (Java 7 release wan’t that big though)
But now, oracle has decided to follow a new Java release schedule. From now Java will be released in every six month cycle.
New Java Release Cycles
Let me review some previous Java release since Java 6
Java 6 (2006):
Released in 2006. It was a Sun Microsystems release. Big changes was adding scripting apis.
Java 7 (2011):
First Oracle release in 2011. Not a big release, considering Oracle took almost 5 years to release a new version.New was Bytecode for dynamic languages and some small changes in language features. Too bad oracle kind of neglected Java during that time.
Java 8 (2014):
Now we are talking. Java came back with a big changes like supporting Lambdas and streams, heading to a functional programming paradigm.
Java 9 (2017):
It was more like an architectural change for Java. Total JDK was modularized. No big fat rt.jar. All of the apis are packaged into modules. Changed the way how we write code in Java.
Java 10 (2018):
In a span of just one year, Java 10 is released in march, 2018. It’ll now be released in 6 months cycle. The new change in this release is Local-Variable Type inference.
Local-Variable Type inference
Introduction to var
If you have used JavaScript of Kotlin before, you may understand the concept of dynamic variable type var.
Java was always a statically typed language. But from Java 10 you can use var on your local variable type.
For example, if we need to declare a variable of type String, we did it like this
String name = "Your name";
But on Java 10, we can do like this.
var name = "your name"
Cool! so does this make Java a dynamic language?
No! absolutely not. If you do not specify the type explicitly, type of that variable will be inferred for you by the compiler according to your value. It is the variable type inference.
Then why is it called Local-Variable type inference?
Because you will only be able to use var in local scope (inside the method)
private void doSomething(){ var name = "Your name" }
You can not use var in method parameter too.
So why is it important?
Suppose we have to write a code to send a request to a server and get a response. Previously, we needed to do like this.
URL url = new URL("https://www.google.com"); URLConnection connection = url.openConnection(); InputStream inputStream = new BufferedInputStream(connection.getInputStream());
Look how we wrote a lot of code on the left. If we used var it’ll look like this
var url = new URL("https://www.google.com"); var urlConnection = url.openConnection(); var dataStream = new BufferedInputStream(connection.getInputStream());
See the difference? it looks pretty and variables are aligned so nicely. But watch carefully as I changed the variable names to meaningful ones because, as we haven’t explicitly mentioned their type, it could get really messy so easily and loose it’s readability. Whenever you use var please make sure variable names are not confusing and anyone can guess it’s type just by looking at the variable name.
Where it can use var and where we can’t
So the basic concept is, we can’t use var in other than local scope. We can’t use it in global scope, neither in method parameters.
And since the variable type is inferred from the value itself, if we want to declare a variable using var, it must be initialized.
var name; // will give error var name = "Sayem"; // String type will be inferred to variable, name var i = 5; // int type will be inferred to variable, i
To declare an array, we can not use var like this
var arr = {1,2,3};
we have to mention the type on the right side. like
var arr = new int[]{1,2,3}
Look at this List declaration
List<String> list = new ArrayList<String>();
Now can we replace it with var?
var list = new ArrayList<String>(); // it will work, the variable type is pretty clear on the right side
but,
var list = new ArrayList<>();
Will it work?
The answer is yes. It’ll still work. But instead of getting ArrayList<String>, Java compiler will provide you ArrayList<Object>. Because there is no type definition for the list.
So we can use var in so many things, that doesn’t mean we should use it in everywhere.
Look at this code,
var length = Product.getProducts().stream().findFirst().map(String::length).orElse(0);
Does it say anything? it’s absolutely horrible piece of code. instead if we used it like that
var products = Product.getProducts() var firstProduct = products.findFirst(); var length = firstProduct.map(String::length).orElse(0)
int this code, var makes it readable and easy to understand.
So the thing is, don’t write code that is hard to understand, and use var appropriately.
Don’t use var if you can’t guess the type by looking at the right side of the statement, or it’s too hard to guess.