JAVA 10 : Local Variable Type Inference
Like JavaScript, Kotlin,
and Scala, now Java 10 introduced var
keyword that allows you to declare a local variable without specifying its
type. The type will be inferred from context. For example, when you say var name = "Silan", then
the compiler will already know the type is String.
The var keyword can only
be used for local variables, i.e. variables inside methods or code blocks — you
cannot use it for member variable declaration inside the class body.
But like Python, it
doesn't make Java a dynamically typed language. Java is still a statically
typed language, and once the type is assigned, you cannot change it. For
example, var name = "Silan" is ok, but if we will assign name =100; then it is not
right.
This is one of the most
eye-catching features of Java 10. It reduces the amount of boilerplate code
needed to declare local variables in Java.
Overall we can say in a
single statement that we can declare variables without specifying the
associated type. A declaration such as:
List<String> l=new
ArrayList<String>();
Can be written as var
l=new ArrayList<String>();
Note: Type Inference is not a
new Concept in JAVA but it is a new concept for local variables.
Lucky Club Online Casino site! | Live! Lucky Club
ReplyDeleteRegister luckyclub your account using the links on the top right corner. A big list of casino sites. Your name will tell you what you want to play.