天天看点

var 局部变量 全局变量_Java SE 9:局部变量为“ var”

var 局部变量 全局变量

Oracle Corporation is going to release Java New Version: Java SE 9 around March 2017. So, I would like to deliver a series of Posts on Java SE 9 New Features. It is my third post in this series.

Oracle Corporation将于2017年3月左右发布Java新版本:Java SE9。因此,我想发表一系列有关Java SE 9新功能的文章。 这是我在本系列文章中的第三篇。

I have already delivered few posts on Java SE 9 New Features. Before going through this posts, please read them below :

我已经发表了有关Java SE 9新功能的几篇文章。 在阅读这篇文章之前,请先阅读以下内容:

  • Java SE 9 REPL

    Java SE 9 REPL

  • Java SE 9: Private methods in Interface

    Java SE 9:接口中的私有方法

In this post, we are going to discuss one more Java SE 9 New Feature: “var” with some simple and suitable examples.

在本文中,我们将讨论一些Java SE 9新功能: “ var” ,并提供一些简单而合适的示例。

局部变量的var (The var for local variables)

Java SE 9 is coming with “var” to define and initialise local variables. Is it a new keyword? We will discuss it in the coming section.

Java SE 9附带了“ var”来定义和初始化局部变量。 是新关键字吗? 我们将在下一节中讨论它。

The main intention of this change in the Java Language is that:

Java语言中此更改的主要目的是:

  • To improve “Type Inference”.

    改善“类型推断”。

  • To reduce verbosity in the code

    减少代码中的冗长

  • To write clean code

    编写干净的代码

Java SE 6:局部变量 (Java SE 6: local variables)

Before Java SE 7, to initialise local variables we use the following approach:

在Java SE 7之前,要初始化局部变量,我们使用以下方法:

Example:-

例:-

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

Here we need to define type parameters at both ends. It looks like some verbose right.

在这里,我们需要在两端定义类型参数。 看起来有些冗长。

Java SE 7:局部变量 (Java SE 7: local variables)

Java SE 7 has introduced a Diamond Operator to reduce some verbosity. It is represented as empty angle brackets: .

Java SE 7引入了Diamond运算符以减少一些详细信息。 它表示为空尖括号: 。

We can use this Diamond Operator at right hand side to avoid the declaring of same type parameters twice as shown below:

我们可以在右侧使用此Diamond运算符来避免两次声明相同类型的参数,如下所示:

Example:-

例:-

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

Here we are reducing some verbosity by avoiding type parameters at right hand side. However, we can observe that still some verbosity is there right.

在这里,我们通过避免在右侧使用类型参数来减少一些冗长的内容。 但是,我们可以看到仍然有一些详细信息。

Java SE 9:局部变量 (Java SE 9: local variables)

To fix this verbosity issue, Java SE 9 is coming with a new feature: “var” to define and initialise the local variables as shown below:

为了解决这个冗长的问题,Java SE 9附带了一个新功能:“ var”,用于定义和初始化局部变量,如下所示:

Example-1:-

示例1:-

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

Here Java 9 Compiler with infer the type of phoneBook reference as new HashMap<String, List<Int>>.

Java 9编译器在这里将电话簿引用的类型推断为新的HashMap <String,List <Int >>。

When we use var identifier like this to define local variables, compiler will infer it’s type automatically.

当我们使用这样的var标识符定义局部变量时,编译器将自动推断其类型。

NOTE:-

The type is inferred based on the type of the initializer. If there is no initializer, the initializer is the null literal, or the type of the initializer is not one that can be normalized to a suitable denotable type.

注意:-

根据初始化程序的类型推断类型。 如果没有初始化程序,则初始化程序为空文字,或者初始化程序的类型不是可以归一化为合适的可表示类型的类型。

Example-2:-

示例2:-

var list = new ArrayList<String>();
           

Here Java 9 Compiler infers the type of list as ArrayList<String>, but not List<String>.

Java 9编译器在这里将列表的类型推断为ArrayList <String>,而不是List <String>。

Example-3:-

示例3:-

var stream = list.stream();
           

Here Java 9 Compiler infers the type of list as Stream<String>.

Java 9编译器在这里将列表的类型推断为Stream <String>。

NOTE:- Oracle Corp. is still working on this feature and they have not finalised about release of this feature in Java SE 9 or in future releases. We will wait for their updates.

注意: -Oracle Corp.仍在使用此功能,他们尚未就Java SE 9或将来版本中的该功能的发布确定最终版本。 我们将等待他们的更新。

var是关键字吗? (Is var a Keyword?)

In Java SE 9, “var” is NOT a keyword. It is a Reserved Type name. That means if our existing code uses var as a variable name, method name, or package name, then they do NOT effect with this change.

在Java SE 9中,“ var”不是关键字。 它是保留类型名称。 这意味着,如果我们现有的代码使用var作为变量名,方法名或程序包名,则它们对此更改无效。

However any class name or interface will affect this change. It is very rare case and not recommended approach to use “var” as a class or interface name so this change does not effect existing code base.

但是,任何类名或接口都会影响此更改。 在极少数情况下,不建议使用“ var”作为类或接口名称,因此此更改不会影响现有代码库。

这项改进的优势 (Advantages of this improvement)

Because of this new feature in Java 9, we will get the following benefits:

由于Java 9中的这一新功能,我们将获得以下好处:

  • Avoid writing boilerplate code

    避免编写样板代码

  • Improve some Readability (Some time reduce Readability).

    提高一些可读性(有时会降低可读性)。

  • Reduce verbosity in the code.

    减少代码中的冗长性。

面试问题 (Interview Questions)

We have already discussed the following Java SE 9 Interview Questions in the above sections.

在以上各节中,我们已经讨论了以下Java SE 9面试问题。

  • What is the use of Java SE 9 var?

    Java SE 9 var的用途是什么?

  • Is var a keyword?

    var是关键字吗?

  • Will this change effect existing code base?

    此更改会影响现有的代码库吗?

  • What is the main use of Java SE 9 var?

    Java SE 9 var的主要用途是什么?

  • How Java SE 9 var improves Type Inference?

    Java SE 9 var如何改善类型推断?

NOTE:-

They are considering val also to include into the language soon.

注意:-

他们正在考虑将val也尽快纳入该语言。

Like Scala supports “var” and “val”, Java 9 is also trying to include those constructs to improved Java Programming language.

像Scala支持“ var”和“ val”一样,Java 9也在尝试将那些结构包括进改进的Java编程语言中。

However, Scala uses:

但是,Scala使用:

  • var to define variables or mutable data

    var定义变量或可变数据

  • val to define values or immutable data

    用于定义值或不可变数据的val

That’s it all about “Java SE 9: var for local variables” concept. We will discuss some more Java SE 9 New Features in my coming posts.

这就是“ Java SE 9:局部变量的var”概念的全部内容。 我们将在以后的文章中讨论更多Java SE 9新功能。

Please drop me a comment if you like my post or have any issues/suggestions/type errors.

如果您喜欢我的帖子或有任何问题/建议/类型错误,请给我评论。

Thank you for reading my tutorials.

感谢您阅读我的教程。

Happy Java SE 9 Learning!

Java SE 9学习愉快!

Java 10 Local Variable Type Inference. Java 10 Local Variable Type Inference中的更新文章。

翻译自: https://www.journaldev.com/12849/javase9-var-for-local-variables

var 局部变量 全局变量