博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
var 局部变量 全局变量_Java SE 9:局部变量为“ var”
阅读量:2533 次
发布时间:2019-05-11

本文共 5655 字,大约阅读时间需要 18 分钟。

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新功能的几篇文章。 在阅读这篇文章之前,请先阅读以下内容:

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
> phoneBook = new HashMap
>();

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
> 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
>();

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
();

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中的这一新功能,我们将获得以下好处:

面试问题 (Interview Questions)

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

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

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使用:

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学习愉快!

. 的更新文章。

翻译自:

var 局部变量 全局变量

转载地址:http://kmlzd.baihongyu.com/

你可能感兴趣的文章
C#_02.14_基础五_.NET类
查看>>
Flask 学习资源
查看>>
Android SDK下载和更新失败的解决方法 分类: Android...
查看>>
MVC2 强类型的 HTML Helper
查看>>
开发 Windows 8 应用 - 0 - windows 8 开发资源
查看>>
生成二维码图片的工具类
查看>>
Surface Pro 4远程桌面分辨率问题
查看>>
【转】包管理器Bower详细讲解
查看>>
JS膏集02
查看>>
程序员三个境界
查看>>
从微信小程序开发者工具源码看实现原理(一)- - 小程序架构设计
查看>>
ASP.NET 上的 Async/Await 简介
查看>>
Effective C++学习笔记(1)
查看>>
element-ui 组件源码分析整理笔记目录
查看>>
GridEh排序
查看>>
[oc学习笔记]多态
查看>>
Tomcat connectionTimeout问题定位处理
查看>>
【PP系列】SAP 取消报工后修改日期
查看>>
ZooKeeper学习第四期---构建ZooKeeper应用(转)
查看>>
JNday4-am
查看>>