简体   繁体   English

在我的 Java 代码中发现奇怪的表达式:变量赋值语法

[英]Strange expression found in my Java code: variable assignment syntax

Somewhere in my code today, I found that I had written the following line今天在我的代码中的某个地方,我发现我写了以下行

JsonArray environmentJsonArray = new JsonArray(), playerJsonArray, teamJsonArray;

I am at a complete loss in understanding how this is even working.我完全不明白这是如何工作的。 The basic question is, What are the two variables on my RHS ?基本问题是,我的 RHS 上的两个变量是什么? I checked if I had declared them before, but I did not.我检查了我之前是否已经声明过它们,但我没有。 The compiler still does not complain apparently indicating that the variables playerJsonArray and teamJsonArray were taken as fresh declarations.编译器仍然没有抱怨显然表明变量playerJsonArrayteamJsonArray被视为新的声明。 But then the declared variables are always on the LHS.但是声明的变量总是在 LHS 上。 Isn't that so?不是这样吗? Has something changed in Java's basic syntax? Java 的基本语法有什么变化吗? Iam trying to go through the specifications to get to the bottom but just in case anyone has a quicker understanding please.我试图通过规范深入了解,但以防万一有人有更快的理解。

You're declaring 3 variables of type JsonArray , and only assigning an initial value to the first.您声明了 3 个JsonArray类型的JsonArray ,并且只为第一个分配了初始值。

It's basically the same as:它基本上与以下相同:

JsonArray environmentJsonArray = new JsonArray();
JsonArray playerJsonArray;
JsonArray teamJsonArray;

The relevant section of JLS 9 is Sec 14.4 . JLS 9 的相关部分是第 14.4 节

Has something changed in Java's basic syntax? Java 的基本语法有什么变化吗?

It has been like this since version 1.0 .1.0 版开始就是这样。

It's similar to declaring 3 variables whose type is JsonArray also you only initialize the first one which is environmentJsonArray.这类似于声明 3 个类型为 JsonArray 的变量,您也只需初始化第一个环境JsonArray。 The remaining ones which are playerJsonArray, teamJsonArray are the uninitialized ones.其余的 playerJsonArray、teamJsonArray 是未初始化的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM