-
Bug
-
Resolution: Duplicate
-
P4
-
None
-
5.0
-
sparc
-
solaris_10
FULL PRODUCT VERSION :
n/a
A DESCRIPTION OF THE PROBLEM :
null is a reference literal, but not a literal of String type. In the
JLS 15.28, constant expression is defined as for literals:
Literals of primitive type and literals of type String
Therefore, null is not considered as constant expression. This means that it cannot be a component in a constant expression, even though there is no reason why it cannot be. Consider the following code:
public class t {
private static final String FOO = "bar";
public static void main(String[] args) {
System.out.println(FOO == null ? 1 : -1);
System.out.println(FOO == "bar" ? 2 : -2);
System.out.println(FOO == "xyz" ? 3 : -3);
switch (args.length) {
/* 1 */ case (FOO == null ? 1 : -1): System.out.println("null"); break;
/* 2 */ case (FOO == "bar" ? 2 : -2): System.out.println("bar"); break;
/* 3 */ case (FOO == "xyz" ? 3 : -3): System.out.println("xyz"); break;
}
}
}
The compiler complains at 1, presumably because null cannot be part of a constant expression. But the complaint about 1 is not reasonable, especially if 2 and 3 are proper. The value of '((String) "bar") == null)' is clearly known at compile time, as clearly as '((String) "bar") == "bar")'. And if you change the value of FOO to be null, you get a different set of compiler errors: All of 1, 2, and 3 are now flagged as non-constant expressions for this reason.
I believe that 15.28 should read:
Literals of primitive type, the null literal, and literals of type String
REPRODUCIBILITY :
This bug can be reproduced always.
n/a
A DESCRIPTION OF THE PROBLEM :
null is a reference literal, but not a literal of String type. In the
JLS 15.28, constant expression is defined as for literals:
Literals of primitive type and literals of type String
Therefore, null is not considered as constant expression. This means that it cannot be a component in a constant expression, even though there is no reason why it cannot be. Consider the following code:
public class t {
private static final String FOO = "bar";
public static void main(String[] args) {
System.out.println(FOO == null ? 1 : -1);
System.out.println(FOO == "bar" ? 2 : -2);
System.out.println(FOO == "xyz" ? 3 : -3);
switch (args.length) {
/* 1 */ case (FOO == null ? 1 : -1): System.out.println("null"); break;
/* 2 */ case (FOO == "bar" ? 2 : -2): System.out.println("bar"); break;
/* 3 */ case (FOO == "xyz" ? 3 : -3): System.out.println("xyz"); break;
}
}
}
The compiler complains at 1, presumably because null cannot be part of a constant expression. But the complaint about 1 is not reasonable, especially if 2 and 3 are proper. The value of '((String) "bar") == null)' is clearly known at compile time, as clearly as '((String) "bar") == "bar")'. And if you change the value of FOO to be null, you get a different set of compiler errors: All of 1, 2, and 3 are now flagged as non-constant expressions for this reason.
I believe that 15.28 should read:
Literals of primitive type, the null literal, and literals of type String
REPRODUCIBILITY :
This bug can be reproduced always.
- duplicates
-
JDK-4083093 JLS should define null as a compile time constant
-
- Closed
-
- relates to
-
JDK-4395280 specification of string concatenation and the null literal
-
- Closed
-