Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does initializing a string in an if statement seem different than in a switch statement? [duplicate]

I'm learning Java and I'm making simple programs to find the season that a month is in, based off some book examples. These two classes demonstrate two ways of testing a value: if/else if statement, and switch statement. The thing I'm confused with is the string that is used to hold the season. When I declare it as just String season; it works with the if statements. But with the switch statement, doing that produces a "The local variable season may not have been initialized" error.

public class IfElse {     public static void main(String args[]) {         int month = 5;         String season;             // isn't initialized, works fine          if(month == 12 || month == 1 || month == 2)             season = "Winter";         else if(month == 3 || month == 4 || month == 5)             season = "Spring";         else if(month == 6 || month == 7 || month == 8)             season = "Summer";         else             season = "Fall";          // this is okay         System.out.println("May is a " + season + " month.");     } } 

Not initializing season at the same time as declaration works fine for the above code, but the season variable in the last println() for the switch produces an error if it's declared the same way.

The following code doesn't work:

public class Switch {     public static void main(String args[]) {         int month = 5;         String season;             // HAS to be initialized, currently causes error         switch(month) {         case(12):         case(1):         case(2):              season = "Winter";             break;         case(3):         case(4):         case(5):              season = "Spring";             break;         case(6):         case(7):         case(8):              season = "Summer";             break;         case(9):         case(10):         case(11):              season = "Fall";             break;          default:             System.out.println("Invalid month");             break;         }         System.out.println("May is a " + season + " month");     }             // produces an error if season isn't initialized to null or "" } 

What causes this? Is it the braces enclosing the switch statement, or a problem with the switch statement itself? How is initializing a string inside an if statement any different than initializing it inside a switch statement? I just can't seem to understand this.

Sorry if this is extremely obvious or if it seems like a dumb question.

like image 225
jkofskie Avatar asked Jan 04 '19 03:01

jkofskie


People also ask

Does switch statement work with strings?

Yes, we can use a switch statement with Strings in Java.

Can switch compare strings?

The switch statement compares the String object in its expression with the expressions associated with each case label as if it were using the String. equals method; consequently, the comparison of String objects in switch statements is case sensitive.

Can we use case as a variable name in Java?

The convention in Java is to always start a variable name with a lower case letter and then uppercase the first letter of each additional word. Variable names can not include spaces so uppercasing the first letter of each additional word makes it easier to read the name.


1 Answers

That is because you did not specify what season has to be in the default case. What happens when month is not within 1-12? season will not be initialized.

If you are expecting strictly only 1-12 as month input, then you might want to consider throwing an Exception in default:

default:     throw new IllegalArgumentException("Invalid month"); 
like image 196
mkjh Avatar answered Sep 23 '22 01:09

mkjh