不是有效的 Groovy 代码的有效 Java 代码? [英] Valid Java code that is NOT valid Groovy code?

查看:19
本文介绍了不是有效的 Groovy 代码的有效 Java 代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大多数 Java 代码在语法上也是有效的 Groovy 代码.但是,有一些例外导致我提出我的问题:

Most Java code is also syntactically valid Groovy code. However, there are a few exceptions which leads me to my question:

Java 中的哪些构造/特性在 Groovy 中在语法上无效?请提供不是有效 Groovy 代码(Groovy 1.6)的 Java 代码(Java 1.6)的具体示例.

Which constructs/features in Java are syntactically invalid in Groovy? Please provide concrete examples of Java code (Java 1.6) that is NOT valid Groovy code (Groovy 1.6).

更新:

到目前为止,我们有五个语法有效的 Java 代码示例,但它们不是有效的 Groovy 代码:

So far we've got five examples of syntactically valid Java code that is not valid Groovy code:

  1. 数组初始化
  2. 内部类
  3. def 在 Groovy 中是一个关键字,但在 Java 中不是
  4. "$$"-strings - 在 Groovy 中被解析为无效的 GString
  5. 非静态初始化块 -- class Foo { Integer x;{ x = 1;} }
  1. Array initializations
  2. Inner classes
  3. def is a keyword in Groovy, but not in Java
  4. "$$"-strings - parsed as an invalid GStrings in Groovy
  5. Non-static initialization blocks -- class Foo { Integer x; { x = 1; } }

这是完整的列表吗?还有其他例子吗?

Is this the complete list? Any further examples?

更新 #1: 我已经开始悬赏来提出这个问题.赏金将授予提供最全面示例列表的人.到目前为止,我们已经发现了五个示例,但我相信还有更多示例.所以让他们来吧!

Update #1: I've started a bounty to bump this question. The bounty will be granted to the person who provides the most comprehensive list of examples. So far we've uncovered five examples, but I'm sure there a quite some more out there. So keep them coming!

推荐答案

这里是有效的 Java 6 但无效的 Groovy 1.6 的项目列表.这不是一个完整的列表,但我认为它涵盖了大多数情况.如下所述,其中一些被后来的 Groovy 版本允许.

Here is a list of items that are valid Java 6, but not valid Groovy 1.6. This isn't a complete list, but I think it covers most of the cases. Some of these are permitted by later Groovy versions as noted below.

(顺便说一句,我认为您应该注意到非静态初始化块在 Groovy 中确实有效.)

(By the way, I think you should note that non-static initialization blocks DO work in Groovy.)

Groovy 1.6 中的任何内部类声明(1.7 添加的内部类):

Any inner class declaration in Groovy 1.6 (1.7 added inner classes):

包括静态,

public class Outer{
  static class Inner{}
}

非静态,

public class Outer{
  class Inner{}
}

本地类,

public class Outer{
  public static void main(String[] args) {
    class Local{}  
  }
}

和匿名类

java.util.EventListener listener=new java.util.EventListener(){};

使用 Groovy 关键字作为变量在任何 Groovy 版本中都不起作用:

int def;
int in;
int threadsafe;
int as;

Java 数组初始化

String[] stuff=new String[]{"string"};
int[] array={1,2,3};

通过将 {...} 更改为 [...] 来使用 Groovy 数组文字格式.

Use the Groovy array-literal format by changing {...} to [...].

在后面不是有效表达式的字符串中使用美元符号

String s="$$";
String s="$def";
String s="$enum";
String s="$;";
String s="$\";
//etc.

一个 for 循环中有多个初始化器

for (int i=0, j=0; i < 5; i++) {}

for 循环中的多个增量

int j=0;
for (int i=0; i < 5; i++,j++) {}

使用换行符分解一些表达式

int a= 2 
/ 2 
;

提示:在 Groovy 中使用反斜杠续行

Hint: use a backslash line-continuation in Groovy

int a= 2 
/ 2 
;

以没有正文的案例结束切换

switch(a){
  case 1:
}

在没有正文的开关中有一个默认值

适用于默认在末尾的两种情况

Applies in both cases where default is at the end

int a=0;
switch(a){
    default:
}

或中间的某个地方

switch(a){
    default:
    case 1:
        break;
}

带列表的注释

@SuppressWarnings({"boxing","cast"})

提示:改用 Groovy 列表字面量语法:

Hint: use Groovy list-literal syntax instead:

@SuppressWarnings(["boxing","cast"])

本地方法声明

public native int nativeMethod();

**1.6 中每个枚举的类(在更高的 Groovy 版本中有效)**

**Class per enum in 1.6 (valid in later Groovy versions) **

public enum JavaEnum{
  ADD{
    public String getSymbol(){ return "+"; }
  };
  abstract String getSymbol();
}

做循环

do{
  System.out.println("stuff");
}while(true);

平等

虽然技术上 == 是有效的 Groovy 和 Java,但它在语义上是不同的.这是您不能仅仅依靠将 Java 编译为 Groovy 而无需更改的原因之一.更糟糕的是,由于 Java 字符串实习,它有时似乎可以工作.

While technically == is valid Groovy and Java, it's semantically different. It's one of the reasons you can't rely on just compiling Java as Groovy without changes. Worse, it might seem to work sometimes due to Java string interning.

示例太长,无法添加到现有答案中,但重点是 语法有效的 Java 代码,因为 Groovy 可能在运行时表现不同.

The example was too long to add to an existing answer, but the point is that Java code that's syntatically valid as Groovy might behave differently at runtime.

要在两个非空对象上获得与 Java 的 x == y 相同的结果,您需要在 Groovy 中使用 x.is(y).x == y 是有效的 Groovy,它只是做一些不同的事情.

To get the same result as Java's x == y for two not-null objects you need x.is(y) in Groovy. x == y is valid Groovy, it just does something different.

Groovy 文档有更详细和更广泛的差异列表.

这篇关于不是有效的 Groovy 代码的有效 Java 代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆