Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Groovy在这段代码中的表现与Java不同_Java_Groovy_Jvm Languages - Fatal编程技术网

Groovy在这段代码中的表现与Java不同

Groovy在这段代码中的表现与Java不同,java,groovy,jvm-languages,Java,Groovy,Jvm Languages,我看了一下这个问题。这个问题的问题是这样的: Integer i3 = (Integer) -128; /*** Doesn't compile ***/ 正如一些答案所说: The compiler interprets the - as the two-arg minus operator, i.e. it's trying to subtract 128 from some other number named Integer, but there's no such variable

我看了一下这个问题。这个问题的问题是这样的:

Integer i3 = (Integer) -128; /*** Doesn't compile ***/
正如一些答案所说:

The compiler interprets the - as the two-arg minus operator, i.e. it's trying to subtract 128 from some other number named Integer, but there's no such variable in scope
答案对我来说似乎是正确的。现在在groovy中,我尝试了与以前相同的代码:

Integer i3 = (Integer) -128; /*** compiles!!! ***/
即使是这一行代码也编译:

Integer i3 = (Integer) -(128); /*** compiles ***/
Groovy如何执行此操作?所有Jvm语言都会这样做吗?在Groovy的案例中,幕后发生了什么

这不违反Java规则吗?有点困惑

作为参考,我已经标记了正在工作的Groovy代码

提前谢谢

Integer i3 = (Integer) -128;
println i3
在groovy 1.8下编译和运行,groovy不是Java

它不必遵循Java的规范,在本例中。。。没有


为清晰起见进行编辑:我想让你困惑的是你不明白这是两种不同的语言。Groovy编译器和Java编译器都从源代码输出字节码,然后在JVM(Java虚拟机)上运行。JLS(Java语言规范)仅适用于Java语言。Groovy不必遵守它

如何解释一段代码中的减号是编译器的一项功能,它实现了一个语言定义。它实际上与代码将在其中实际执行的运行时系统(在本例中是JVM)没有关系。因此,不同的语言可以有相同的符号/关键字等。它们的行为方式不同。

重新阅读他的问题。他指出,在Java中,这是不可编译的。他不明白Java和Groovy不是一回事儿。@StephenC如果他们坐着的话就不是了。Groovy通过(稍微)不同的语法来“执行那个操作”。