java全局变量和局部变量_Java 10:局部变量类型推断

java全局变量和局部变量

In this article, we would take a deep dive at the new feature of Local-Variable Type Inference introduced in Java 10. We will go through the scope and limitations of using the local variable type inference.

在本文中,我们将深入研究Java 10中引入的Local-Variable Type Inference的新功能。我们将探讨使用局部变量类型推断的范围和局限性。

This feature was proposed as part of JEP (JDK Enhancement Proposal): 286. The proposal was for enhancing the language to support the type inference to local variable declaration and initialization.

此功能是作为JEP(JDK增强建议):286的一部分提出的。 该提案是为了增强语言以支持对局部变量声明和初始化的类型推断。

1. Java 10:局部变量类型推断 (1. Java 10: Local Variable Type Inference)

With Java 10, you can use var for local variables instead of a typed name (Manifest Type). This is done by a new feature which is called Local Variable Type Inference.

使用Java 10,您可以将var用作局部变量,而不是使用类型名称(清单类型)。 这是通过一项称为局部变量类型推断的新功能完成的。

But first, What is Type Inference?

但是首先, 什么是类型推断?

Type inference is Java compiler’s ability to look at each method invocation and corresponding declaration to determine the type argument (or arguments) that make the invocation applicable. Type Inference is not to Java programming.

类型推断是Java编译器查看每个方法调用和相应声明以确定使调用适用的类型参数的能力。 类型推断不适用于Java编程。

For local variable declarations with initializer, we can now use a reserved type name “var” instead of a manifest type. Let’s look through a few examples.

对于带有初始化程序的局部变量声明,我们现在可以使用保留的类型名称“ var”而不是清单类型。 让我们看一些例子。

var list = new ArrayList<String>(); // infers ArrayList<String>
var stream = list.stream();         // infers Stream<String>

Manifest Type: Explicit identification of type for each variable being declared is called as Manifest Typing. For example, If a variable “actors” is going to store a List of Actors, then its type List<Actor> is the manifest type and its must be declared (as mentioned below) prior to Java 10:

清单类型 :对要声明的每个变量的类型的显式标识称为清单类型。 例如,如果变量“ actors”将存储一个Actor列表,则其类型List <Actor>是清单类型,并且必须在Java 10之前声明它(如下所述):

List<Actor> actors =  List.of(new Actor()); // Pre Java 10 
var actors = List.of(new Actor()); // Java 10 onwards

2.局部变量类型推断如何工作? (2. How does Local Variable Type Inference work?)

Parsing a var statement, the compiler looks at the right-hand side of the declaration, aka initializer, and it infers the type from the right-hand side (RHS) expression.

解析var语句后,编译器将查看声明的右侧(也称为初始化程序),并从右侧(RHS)表达式推断类型。

Ok fine enough, does this mean that now Java is a dynamically typed language? Not really, it’s still a statically typed language. Let’s take a code snippet for reading a file.

好的,这是否意味着Java现在是一种动态类型的语言? 并非如此,它仍然是静态类型的语言。 让我们以一个代码片段来读取文件。

private static void readFile() throws IOException {
	var fileName = "Sample.txt";
	var line = "";
	var fileReader = new FileReader(fileName);
	var bufferedReader = new BufferedReader(fileReader);
	while ((line = bufferedReader.readLine()) != null) {
		System.out.println(line);
	}
	buffere
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值