Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7857679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:06:39+00:00 2026-06-02T21:06:39+00:00

Integer i = … switch (i) { case null: doSomething0(); break; } In the

  • 0
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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T21:06:42+00:00Added an answer on June 2, 2026 at 9:06 pm

    This was not possible with a switch statement in Java until Java 18. You had to check for null before the switch. But now, with pattern matching, this is a thing of the past. Have a look at JEP 420:

    Pattern matching and null

    Traditionally, switch statements and expressions throw
    NullPointerException if the selector expression evaluates to null, so
    testing for null must be done outside of the switch:

    static void testFooBar(String s) {
         if (s == null) {
             System.out.println("oops!");
             return;
         }
         switch (s) {
             case "Foo", "Bar" -> System.out.println("Great");
             default           -> System.out.println("Ok");
         }
     }
    

    This was reasonable when switch supported only a few reference types.
    However, if switch allows a selector expression of any type, and case
    labels can have type patterns, then the standalone null test feels
    like an arbitrary distinction, and invites needless boilerplate and
    opportunity for error. It would be better to integrate the null test
    into the switch:

    static void testFooBar(String s) {
        switch (s) {
            case null         -> System.out.println("Oops");
            case "Foo", "Bar" -> System.out.println("Great");
            default           -> System.out.println("Ok");   
      }
    }
    

    More about switch (including an example with a null variable) in Oracle Docs – Switch

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Integer c=0; DatabaseHelper dbh1 = new DatabaseHelper(this); Cursor ca = dbh1.getReadableDatabase().query(DatabaseHelper.TABLE_NAME, null, null, null,
try{Integer.parse(Abhishek);} catch(NumberFormatException e){} catch(Exception e){} If for the above piece of code, if NumberFormatException
Integer i = null; if (i == 3) Why the second line above throws
Integer n = 5; System.out.println(n) // 5! How can i reproduce this behavior in
final Integer[] arr={1,2,3}; arr[0]=3; System.out.println(Arrays.toString(arr)); I tried the above code to see whether a
I want to give integer input to Ruby like this: 12 343 12312 12312
Integer and Float use 4 bytes of memory for storage. However the range of
Possible Duplicate: php validate integer Currently I'm using this method: Validate the input using
Integer.parseInt(ff8ca87c, 16); This gives me a NumberFormatException for some reason. Do you know why
Which out of the INTEGER and VARCHAR datatypes is better for use as primary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.