简体   繁体   中英

does default case MUST be included in switch statements in Java

I was just wondering if default case MUST be included in switch statements in Java. I understand it is good practice to include default cases. The reason why I ask is because for the code below, if I delete default case, the code will provide error. Could someone please help me clarify the concept? Thanks in advance for any help!

public class SwitchDemo {
    public static void main(String[] args) {

        int month = 8;
        String monthString;
        switch (month) {
            case 1:  monthString = "January";
                     break;
            case 2:  monthString = "February";
                     break;
            case 3:  monthString = "March";
                     break;
            case 4:  monthString = "April";
                     break;
            case 5:  monthString = "May";
                     break;
            case 6:  monthString = "June";
                     break;
            case 7:  monthString = "July";
                     break;
            case 8:  monthString = "August";
                     break;
            case 9:  monthString = "September";
                     break;
            case 10: monthString = "October";
                     break;
            case 11: monthString = "November";
                     break;
            case 12: monthString = "December";
                     break;
            default: monthString = "Invalid month"; //if delete will produce error
                     break;
        }
        System.out.println(monthString);
    }
}

While the default clause is not mandatory, if you remove it, monthString may not be initialized, so you get a compilation error when you attempt to print it with System.out.println(monthString); .

You can remove the default clause if you give monthString a default value when you declare it. For example :

String monthString = "Invalid month";

This will give the same behavior as your current switch statement, which includes the default clause.

It is not required to have a default case, but it is a good idea to have one, since this catches cases that you intentionally do not want to handle (or unintentionally didn't handle).

As a point of reference, the Google Java style guide requires a default case.

In your code, assuming you are free of the strictures of a particular style rule requiring a default case, you don't need it; you just need to definitely assign monthString before you can use it (a requirement in the language specification). However, it doesn't make sense to assign a value for a month value outside the range 1-12 - it is logically wrong.

Throwing an exception is a sensible thing to do here, eg:

default:
  throw new IllegalArgumentException("Invalid month");

If you looks your program you will find your error by own because of Initialization error.

Here

 int month = 8;
 String monthString; //it is a local Variable and not Initialized.

monthString is not initialized at the time of declaration and this Variable doesn't have global access (not a global Variable) so that it can initialize using any Constructor or any other source.

In your switch statement in all your case you have initialize the variable monthString . If you consider each case and default as a differen branch then you will realize that in all possible branch this code is getting executed. In each possibility variable monthString is initialized but If you Remove this code

default: monthString = "Invalid month"; //if delete will produce error
                     break;

then there may be a possibility of a default case ( if all cases are getting Failed ). So, Indirectly you are not consider this default case but there is a possibility of that.

If you still want to remove default case then Initialize your variable monthString at the time of declaration. It will also remove your Compile time error.

This is the reason of getting error after removing the default keyword.

Thank you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM