This is the mail archive of the java@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

System.out.println("foo" + s != null ? s : ""); precedence


With today's trunk, the ternary operator's precedence is wrong:

public class Test {
    public static void main(String[] args) {
        String s = null;
        System.out.println("foo" + s != null ? s : "");
    }
}

$ ./test
null

When enclosed in parentheses, it behaves normally:

    System.out.println("foo" + (s != null ? s : ""));

$ ./test
foo

A core dump happens when this is done in a toString method:

public class Test {
    public static void main(String[] args) {
        System.out.println(new Test());
    }

    public String toString() {
        String s = null;
        return "foo" + s != null ? s : "";
    }
}

$ ./test
Aborted (core dumped)

If the code's more complex it can fail in other strange ways: gdb reported
SIGSEGVs, etc.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]