<p dir="ltr">Isn't null a value and not a type?</p>
<br><div class="gmail_quote gmail_quote_container"><div dir="ltr" class="gmail_attr">On Tue, Jan 28, 2025, 9:25 AM Stephan Herrmann <<a href="mailto:stephan.herrmann@berlin.de">stephan.herrmann@berlin.de</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Assume this (nonsense) program, which is accepted by javac and ecj:<br>
<br>
public class X {<br>
        public static void main(String[] args) {<br>
                switch (null) {<br>
                        case null -> System.out.println("Null");<br>
                        default-> System.out.println("Default");<br>
                }<br>
        }<br>
}<br>
<br>
14.1.1 Switch Blocks requires (T is the type of the selector expression):<br>
* If a null literal is associated with the switch block, then T is a reference type.<br>
<br>
In our case the selector expression has the null type, which can be assigned to <br>
any reference type, but it doesn't seem to be a reference type itself.<br>
<br>
 From this I would conclude that the program is illegal.<br>
<br>
<br>
Next, lets remove the default case from the same program. Now compilers <br>
complain, here's how javac puts it:<br>
<br>
X.java:3: error: the switch statement does not cover all possible input values<br>
                 switch (null) {<br>
                 ^<br>
1 error<br>
<br>
In previous discussions I learned that requiring a default label may be <br>
justified by requirements of future changes, but here for this reasoning to hold <br>
the null type would need to acquire new values, which seems very unlikely :). <br>
So, if the initial program would get the blessing from JLS, then also this case <br>
(without default) deserves a second look.<br>
<br>
thanks,<br>
Stephan<br>
</blockquote></div>