This is the mail archive of the
java@gcc.gnu.org
mailing list for the Java project.
System.out.println("foo" + s != null ? s : ""); precedence
- To: java at gcc dot gnu dot org
- Subject: System.out.println("foo" + s != null ? s : ""); precedence
- From: Mark J Roberts <mjr at anarcast dot net>
- Date: Mon, 13 Aug 2001 14:21:35 -0500
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.