Integer i = ...
switch (i) {
case null:
doSomething0();
break;
}
In the code above I can’t use null in the switch case statement. How can I do this differently? I can’t use default because then I want to do something else.
This was not possible with a
switchstatement in Java until Java 18. You had to check fornullbefore theswitch. But now, with pattern matching, this is a thing of the past. Have a look at JEP 420:More about
switch(including an example with anullvariable) in Oracle Docs – Switch