简体   繁体   中英

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.
The error goes away, when I either

  • make bar() non-generic (just for curiosity, not really an option)
  • remove the parentheses around the ternary expression on line 7

So it looks like that if e is an expression, it is not always valid to write (e) ?

The posted code compiles (and runs) just fine for me using Eclipse, but I can confirm that javac fails to compile this. I suspect you've found a compiler bug in 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.

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. However, it is fixed in jdk 1.7 beta 14 (the developer preview is b185, so it is fixed there, I've tried it).

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     
        )
        ;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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