简体   繁体   English

javac奇怪的语法 - 错误非法启动表达式

[英]javac strange syntax - error illegal start of expression

I encountered a strange error, which I believe is a bug. 我遇到了一个奇怪的错误,我认为这是一个错误。 Here is a minimal case, please do not comment on the usefulness of the code :) 这是一个小例,请不要评论代码的用处:)

class Foo {

    static public <X> int bar() { return 42; }

    public int baz() {
        return true ? 42 : (
            Foo.<Void>bar() > 42 ? 41 : 43
        )
        ;
    }
}

Result: 结果:

err.java:7: illegal start of expression
        Foo.<Void>bar() > 42 ? 41 : 43
            ^

I have tried SUN SDK javac 1.6.0_13 and 1.6.0_21. 我试过SUN SDK javac 1.6.0_13和1.6.0_21。
The error goes away, when I either 当我要么,错误消失了

  • make bar() non-generic (just for curiosity, not really an option) 使bar()非通用(只是为了好奇,不是真正的选择)
  • remove the parentheses around the ternary expression on line 7 删除第7行三元表达式周围的括号

So it looks like that if e is an expression, it is not always valid to write (e) ? 所以看起来如果e是一个表达式,那么写(e)并不总是有效的?

The posted code compiles (and runs) just fine for me using Eclipse, but I can confirm that javac fails to compile this. 发布的代码使用Eclipse编译(并运行)对我很好,但我可以确认javac无法编译它。 I suspect you've found a compiler bug in javac . 我怀疑你在javac发现了编译器错误。

It would probably be a good idea to report it . 报告它可能是个好主意。

I managed to compile it with a little change in the code.So,I guess that it is something to do with conditional operator specification(which is bit complex) or a bug.But this problem occurs only in conditional operator. 我设法通过代码中的一点改变来编译它。所以,我想这与条件运算符规范(有点复杂)或bug有关。但这个问题只出现在条件运算符中。

class Foo {

    static public <X> int bar() { return 42; }

    public int baz() {
        return true ? 42 : (
            ((int)Foo.<Void>bar()) > 42 ? 41 : 43
        );
    }
}

The bug is already some 3 years old, but won't be fixed in jdk 1.6 apparently. 这个bug已经有3年了,但显然不会在jdk 1.6中修复。 However, it is fixed in jdk 1.7 beta 14 (the developer preview is b185, so it is fixed there, I've tried it). 但是,它在jdk 1.7 beta 14中修复(开发人员预览是b185,因此它已修复,我已经尝试过了)。

is the return Value, you don't have to specify this by calling a static method: 是返回值,您不必通过调用静态方法来指定它:

class Foo {

    static public <X> int bar() { return 42; }

    public int baz() {
        return true ? 42 : (
            Foo.bar() > 42 ? 41 : 43     
        )
        ;
    }
}

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

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