Why does initializing a string in an if statement seem different than in a switch statement? [duplicate]
Clash Royale CLAN TAG#URR8PPP
This question already has an answer here:
Why do I get a “variable might not have been initialized” compiler error in my switch block?
1 answer
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) month == 4
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.
java string if-statement switch-statement
marked as duplicate by jww, iBug, pirho, Michael
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Jan 4 at 18:01
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Why do I get a “variable might not have been initialized” compiler error in my switch block?
1 answer
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) month == 4
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.
java string if-statement switch-statement
marked as duplicate by jww, iBug, pirho, Michael
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Jan 4 at 18:01
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
In the code that doesn't work, which season is month 13?
– immibis
Jan 4 at 5:13
2
Meta discussion about this question.
– jpmc26
Jan 4 at 8:02
1
FYI there's no need for brackets around the case values.case 12:
etc is fine.
– Boann
Jan 4 at 13:58
1
BTW it is a good idea to use uninitialized variables like this as it makes it clear if you have considered all path (however as you if example shows you might not have considered it correctly). (Opposed to usingString season = ““;
which is often seen in code from less experienced programmers) and i think upcoming case expressions will make this a better experience.
– eckes
Jan 4 at 16:41
1
Just to be explicit (since none of the answers is): this is unrelated toif
vsswitch
. Your two pieces of code are simply not equivalent, but you can write equivalent code (that exhibits either behaviour) with bothif
andswitch
.
– Konrad Rudolph
Jan 4 at 19:17
add a comment |
This question already has an answer here:
Why do I get a “variable might not have been initialized” compiler error in my switch block?
1 answer
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) month == 4
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.
java string if-statement switch-statement
This question already has an answer here:
Why do I get a “variable might not have been initialized” compiler error in my switch block?
1 answer
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) month == 4
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.
This question already has an answer here:
Why do I get a “variable might not have been initialized” compiler error in my switch block?
1 answer
java string if-statement switch-statement
java string if-statement switch-statement
edited Jan 4 at 13:56
Boann
36.8k1288121
36.8k1288121
asked Jan 4 at 3:21
jkofskiejkofskie
14217
14217
marked as duplicate by jww, iBug, pirho, Michael
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Jan 4 at 18:01
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by jww, iBug, pirho, Michael
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Jan 4 at 18:01
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
In the code that doesn't work, which season is month 13?
– immibis
Jan 4 at 5:13
2
Meta discussion about this question.
– jpmc26
Jan 4 at 8:02
1
FYI there's no need for brackets around the case values.case 12:
etc is fine.
– Boann
Jan 4 at 13:58
1
BTW it is a good idea to use uninitialized variables like this as it makes it clear if you have considered all path (however as you if example shows you might not have considered it correctly). (Opposed to usingString season = ““;
which is often seen in code from less experienced programmers) and i think upcoming case expressions will make this a better experience.
– eckes
Jan 4 at 16:41
1
Just to be explicit (since none of the answers is): this is unrelated toif
vsswitch
. Your two pieces of code are simply not equivalent, but you can write equivalent code (that exhibits either behaviour) with bothif
andswitch
.
– Konrad Rudolph
Jan 4 at 19:17
add a comment |
2
In the code that doesn't work, which season is month 13?
– immibis
Jan 4 at 5:13
2
Meta discussion about this question.
– jpmc26
Jan 4 at 8:02
1
FYI there's no need for brackets around the case values.case 12:
etc is fine.
– Boann
Jan 4 at 13:58
1
BTW it is a good idea to use uninitialized variables like this as it makes it clear if you have considered all path (however as you if example shows you might not have considered it correctly). (Opposed to usingString season = ““;
which is often seen in code from less experienced programmers) and i think upcoming case expressions will make this a better experience.
– eckes
Jan 4 at 16:41
1
Just to be explicit (since none of the answers is): this is unrelated toif
vsswitch
. Your two pieces of code are simply not equivalent, but you can write equivalent code (that exhibits either behaviour) with bothif
andswitch
.
– Konrad Rudolph
Jan 4 at 19:17
2
2
In the code that doesn't work, which season is month 13?
– immibis
Jan 4 at 5:13
In the code that doesn't work, which season is month 13?
– immibis
Jan 4 at 5:13
2
2
Meta discussion about this question.
– jpmc26
Jan 4 at 8:02
Meta discussion about this question.
– jpmc26
Jan 4 at 8:02
1
1
FYI there's no need for brackets around the case values.
case 12:
etc is fine.– Boann
Jan 4 at 13:58
FYI there's no need for brackets around the case values.
case 12:
etc is fine.– Boann
Jan 4 at 13:58
1
1
BTW it is a good idea to use uninitialized variables like this as it makes it clear if you have considered all path (however as you if example shows you might not have considered it correctly). (Opposed to using
String season = ““;
which is often seen in code from less experienced programmers) and i think upcoming case expressions will make this a better experience.– eckes
Jan 4 at 16:41
BTW it is a good idea to use uninitialized variables like this as it makes it clear if you have considered all path (however as you if example shows you might not have considered it correctly). (Opposed to using
String season = ““;
which is often seen in code from less experienced programmers) and i think upcoming case expressions will make this a better experience.– eckes
Jan 4 at 16:41
1
1
Just to be explicit (since none of the answers is): this is unrelated to
if
vs switch
. Your two pieces of code are simply not equivalent, but you can write equivalent code (that exhibits either behaviour) with both if
and switch
.– Konrad Rudolph
Jan 4 at 19:17
Just to be explicit (since none of the answers is): this is unrelated to
if
vs switch
. Your two pieces of code are simply not equivalent, but you can write equivalent code (that exhibits either behaviour) with both if
and switch
.– Konrad Rudolph
Jan 4 at 19:17
add a comment |
4 Answers
4
active
oldest
votes
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");
6
And to point out a small bug in the question askers if/else implementation, the else will set any invalid month toFall
. If month was set to 42, it would printFall
.
– DrZoo
Jan 4 at 3:30
3
@DrZoo You're completely right. I did that out of laziness because I knew I would never set the month value out of range, but that also meant that there would never be a situation where season is uninitialized. Changing the else to an else if also gave me the same error as in the switch statement. So I now realize my problem was with when the variable is (or isn't) being given a value.
– jkofskie
Jan 4 at 3:40
@jkofskie I do not want to be as mean as this now sounds, but the problem was that you ASSUMED the value will never be above 12. Never assume anything in programming, when you start assuming things (especially user input) you get yourself in problems (and in this case the compiler was nice enough to warn you).
– Kami Kaze
Jan 4 at 14:06
@KamiKaze some assumption is useful, though. If you really know that the input value is guaranteed to be in the acceptable range, it shouldn't be necessary to add a guard clause. Defending against conditions that you know will not arise can lead to unreadably complex code. The point here is that the programmer should be careful to separate what the programmer knows about the program from what the compiler knows, or should be expected to know.
– phoog
Jan 4 at 16:33
add a comment |
In your first example, there is no path through the code that fails to assign a value to 'season'. In the second example, the default case does not assign a value, so the last print ("May is...") can be executed with an uninitialized value.
Yeah, for some reason I subconsciously assumed that would only be an issue if the value for month was actually out of the defined range, but I forgot Java doesn't work like that.
– jkofskie
Jan 4 at 4:42
add a comment |
In your if
/else
code, there is an assurance that the variable season
will get a value. That is, the else
statement.
Your switch
code does not have it. Look what will happen to the variable season
if the given value for month is 13
-- it will not get a value, and will remain un-initialised.
add a comment |
You should use this
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:
season = "Invalid";
break;
System.out.println("May is a " + season + " month");
// produces an error if season isn't initialized to null or ""
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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");
6
And to point out a small bug in the question askers if/else implementation, the else will set any invalid month toFall
. If month was set to 42, it would printFall
.
– DrZoo
Jan 4 at 3:30
3
@DrZoo You're completely right. I did that out of laziness because I knew I would never set the month value out of range, but that also meant that there would never be a situation where season is uninitialized. Changing the else to an else if also gave me the same error as in the switch statement. So I now realize my problem was with when the variable is (or isn't) being given a value.
– jkofskie
Jan 4 at 3:40
@jkofskie I do not want to be as mean as this now sounds, but the problem was that you ASSUMED the value will never be above 12. Never assume anything in programming, when you start assuming things (especially user input) you get yourself in problems (and in this case the compiler was nice enough to warn you).
– Kami Kaze
Jan 4 at 14:06
@KamiKaze some assumption is useful, though. If you really know that the input value is guaranteed to be in the acceptable range, it shouldn't be necessary to add a guard clause. Defending against conditions that you know will not arise can lead to unreadably complex code. The point here is that the programmer should be careful to separate what the programmer knows about the program from what the compiler knows, or should be expected to know.
– phoog
Jan 4 at 16:33
add a comment |
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");
6
And to point out a small bug in the question askers if/else implementation, the else will set any invalid month toFall
. If month was set to 42, it would printFall
.
– DrZoo
Jan 4 at 3:30
3
@DrZoo You're completely right. I did that out of laziness because I knew I would never set the month value out of range, but that also meant that there would never be a situation where season is uninitialized. Changing the else to an else if also gave me the same error as in the switch statement. So I now realize my problem was with when the variable is (or isn't) being given a value.
– jkofskie
Jan 4 at 3:40
@jkofskie I do not want to be as mean as this now sounds, but the problem was that you ASSUMED the value will never be above 12. Never assume anything in programming, when you start assuming things (especially user input) you get yourself in problems (and in this case the compiler was nice enough to warn you).
– Kami Kaze
Jan 4 at 14:06
@KamiKaze some assumption is useful, though. If you really know that the input value is guaranteed to be in the acceptable range, it shouldn't be necessary to add a guard clause. Defending against conditions that you know will not arise can lead to unreadably complex code. The point here is that the programmer should be careful to separate what the programmer knows about the program from what the compiler knows, or should be expected to know.
– phoog
Jan 4 at 16:33
add a comment |
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");
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");
edited Jan 5 at 8:56
Peter Mortensen
13.5k1984111
13.5k1984111
answered Jan 4 at 3:24
mkjhmkjh
885719
885719
6
And to point out a small bug in the question askers if/else implementation, the else will set any invalid month toFall
. If month was set to 42, it would printFall
.
– DrZoo
Jan 4 at 3:30
3
@DrZoo You're completely right. I did that out of laziness because I knew I would never set the month value out of range, but that also meant that there would never be a situation where season is uninitialized. Changing the else to an else if also gave me the same error as in the switch statement. So I now realize my problem was with when the variable is (or isn't) being given a value.
– jkofskie
Jan 4 at 3:40
@jkofskie I do not want to be as mean as this now sounds, but the problem was that you ASSUMED the value will never be above 12. Never assume anything in programming, when you start assuming things (especially user input) you get yourself in problems (and in this case the compiler was nice enough to warn you).
– Kami Kaze
Jan 4 at 14:06
@KamiKaze some assumption is useful, though. If you really know that the input value is guaranteed to be in the acceptable range, it shouldn't be necessary to add a guard clause. Defending against conditions that you know will not arise can lead to unreadably complex code. The point here is that the programmer should be careful to separate what the programmer knows about the program from what the compiler knows, or should be expected to know.
– phoog
Jan 4 at 16:33
add a comment |
6
And to point out a small bug in the question askers if/else implementation, the else will set any invalid month toFall
. If month was set to 42, it would printFall
.
– DrZoo
Jan 4 at 3:30
3
@DrZoo You're completely right. I did that out of laziness because I knew I would never set the month value out of range, but that also meant that there would never be a situation where season is uninitialized. Changing the else to an else if also gave me the same error as in the switch statement. So I now realize my problem was with when the variable is (or isn't) being given a value.
– jkofskie
Jan 4 at 3:40
@jkofskie I do not want to be as mean as this now sounds, but the problem was that you ASSUMED the value will never be above 12. Never assume anything in programming, when you start assuming things (especially user input) you get yourself in problems (and in this case the compiler was nice enough to warn you).
– Kami Kaze
Jan 4 at 14:06
@KamiKaze some assumption is useful, though. If you really know that the input value is guaranteed to be in the acceptable range, it shouldn't be necessary to add a guard clause. Defending against conditions that you know will not arise can lead to unreadably complex code. The point here is that the programmer should be careful to separate what the programmer knows about the program from what the compiler knows, or should be expected to know.
– phoog
Jan 4 at 16:33
6
6
And to point out a small bug in the question askers if/else implementation, the else will set any invalid month to
Fall
. If month was set to 42, it would print Fall
.– DrZoo
Jan 4 at 3:30
And to point out a small bug in the question askers if/else implementation, the else will set any invalid month to
Fall
. If month was set to 42, it would print Fall
.– DrZoo
Jan 4 at 3:30
3
3
@DrZoo You're completely right. I did that out of laziness because I knew I would never set the month value out of range, but that also meant that there would never be a situation where season is uninitialized. Changing the else to an else if also gave me the same error as in the switch statement. So I now realize my problem was with when the variable is (or isn't) being given a value.
– jkofskie
Jan 4 at 3:40
@DrZoo You're completely right. I did that out of laziness because I knew I would never set the month value out of range, but that also meant that there would never be a situation where season is uninitialized. Changing the else to an else if also gave me the same error as in the switch statement. So I now realize my problem was with when the variable is (or isn't) being given a value.
– jkofskie
Jan 4 at 3:40
@jkofskie I do not want to be as mean as this now sounds, but the problem was that you ASSUMED the value will never be above 12. Never assume anything in programming, when you start assuming things (especially user input) you get yourself in problems (and in this case the compiler was nice enough to warn you).
– Kami Kaze
Jan 4 at 14:06
@jkofskie I do not want to be as mean as this now sounds, but the problem was that you ASSUMED the value will never be above 12. Never assume anything in programming, when you start assuming things (especially user input) you get yourself in problems (and in this case the compiler was nice enough to warn you).
– Kami Kaze
Jan 4 at 14:06
@KamiKaze some assumption is useful, though. If you really know that the input value is guaranteed to be in the acceptable range, it shouldn't be necessary to add a guard clause. Defending against conditions that you know will not arise can lead to unreadably complex code. The point here is that the programmer should be careful to separate what the programmer knows about the program from what the compiler knows, or should be expected to know.
– phoog
Jan 4 at 16:33
@KamiKaze some assumption is useful, though. If you really know that the input value is guaranteed to be in the acceptable range, it shouldn't be necessary to add a guard clause. Defending against conditions that you know will not arise can lead to unreadably complex code. The point here is that the programmer should be careful to separate what the programmer knows about the program from what the compiler knows, or should be expected to know.
– phoog
Jan 4 at 16:33
add a comment |
In your first example, there is no path through the code that fails to assign a value to 'season'. In the second example, the default case does not assign a value, so the last print ("May is...") can be executed with an uninitialized value.
Yeah, for some reason I subconsciously assumed that would only be an issue if the value for month was actually out of the defined range, but I forgot Java doesn't work like that.
– jkofskie
Jan 4 at 4:42
add a comment |
In your first example, there is no path through the code that fails to assign a value to 'season'. In the second example, the default case does not assign a value, so the last print ("May is...") can be executed with an uninitialized value.
Yeah, for some reason I subconsciously assumed that would only be an issue if the value for month was actually out of the defined range, but I forgot Java doesn't work like that.
– jkofskie
Jan 4 at 4:42
add a comment |
In your first example, there is no path through the code that fails to assign a value to 'season'. In the second example, the default case does not assign a value, so the last print ("May is...") can be executed with an uninitialized value.
In your first example, there is no path through the code that fails to assign a value to 'season'. In the second example, the default case does not assign a value, so the last print ("May is...") can be executed with an uninitialized value.
answered Jan 4 at 3:26
another-daveanother-dave
6295
6295
Yeah, for some reason I subconsciously assumed that would only be an issue if the value for month was actually out of the defined range, but I forgot Java doesn't work like that.
– jkofskie
Jan 4 at 4:42
add a comment |
Yeah, for some reason I subconsciously assumed that would only be an issue if the value for month was actually out of the defined range, but I forgot Java doesn't work like that.
– jkofskie
Jan 4 at 4:42
Yeah, for some reason I subconsciously assumed that would only be an issue if the value for month was actually out of the defined range, but I forgot Java doesn't work like that.
– jkofskie
Jan 4 at 4:42
Yeah, for some reason I subconsciously assumed that would only be an issue if the value for month was actually out of the defined range, but I forgot Java doesn't work like that.
– jkofskie
Jan 4 at 4:42
add a comment |
In your if
/else
code, there is an assurance that the variable season
will get a value. That is, the else
statement.
Your switch
code does not have it. Look what will happen to the variable season
if the given value for month is 13
-- it will not get a value, and will remain un-initialised.
add a comment |
In your if
/else
code, there is an assurance that the variable season
will get a value. That is, the else
statement.
Your switch
code does not have it. Look what will happen to the variable season
if the given value for month is 13
-- it will not get a value, and will remain un-initialised.
add a comment |
In your if
/else
code, there is an assurance that the variable season
will get a value. That is, the else
statement.
Your switch
code does not have it. Look what will happen to the variable season
if the given value for month is 13
-- it will not get a value, and will remain un-initialised.
In your if
/else
code, there is an assurance that the variable season
will get a value. That is, the else
statement.
Your switch
code does not have it. Look what will happen to the variable season
if the given value for month is 13
-- it will not get a value, and will remain un-initialised.
answered Jan 4 at 3:25
KaNa0011KaNa0011
489311
489311
add a comment |
add a comment |
You should use this
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:
season = "Invalid";
break;
System.out.println("May is a " + season + " month");
// produces an error if season isn't initialized to null or ""
add a comment |
You should use this
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:
season = "Invalid";
break;
System.out.println("May is a " + season + " month");
// produces an error if season isn't initialized to null or ""
add a comment |
You should use this
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:
season = "Invalid";
break;
System.out.println("May is a " + season + " month");
// produces an error if season isn't initialized to null or ""
You should use this
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:
season = "Invalid";
break;
System.out.println("May is a " + season + " month");
// produces an error if season isn't initialized to null or ""
answered Jan 4 at 3:24
Alperen GezginAlperen Gezgin
412
412
add a comment |
add a comment |
2
In the code that doesn't work, which season is month 13?
– immibis
Jan 4 at 5:13
2
Meta discussion about this question.
– jpmc26
Jan 4 at 8:02
1
FYI there's no need for brackets around the case values.
case 12:
etc is fine.– Boann
Jan 4 at 13:58
1
BTW it is a good idea to use uninitialized variables like this as it makes it clear if you have considered all path (however as you if example shows you might not have considered it correctly). (Opposed to using
String season = ““;
which is often seen in code from less experienced programmers) and i think upcoming case expressions will make this a better experience.– eckes
Jan 4 at 16:41
1
Just to be explicit (since none of the answers is): this is unrelated to
if
vsswitch
. Your two pieces of code are simply not equivalent, but you can write equivalent code (that exhibits either behaviour) with bothif
andswitch
.– Konrad Rudolph
Jan 4 at 19:17