Page 1 of 1
About Question enthuware.ocajp.i.v7.2.1119 :
Posted: Tue Mar 05, 2013 10:59 am
by Ambiorix
Code: Select all
public class Switcher{
public static void main(String[] args){
switch(Integer.parseInt(args[1])) //1
{
case 0 :
boolean b = false;
break;
case 1 :
b = true; //2
break;
}
if(b) System.out.println(args[2]);
}
}
I was surprised that there is no compilation error at //2 since b isn't declared as a boolean.
While it is declared in case 0:, these lines won't execute if args[1] is 1, so how does the application know that b should be a boolean where it matches case 1:?
I tried commenting out the problematic print line and declaring boolean b = true; at //2, but the compiler complained that b is already defined.
Can I assume that any declarations in the first case are visible to all the other cases?
Also, what happens if I declare a variable in one of the later cases? Is this visible from anywhere in the switch, or only in the cases that follow it?
Re: About Question enthuware.ocajp.i.v7.2.1119 :
Posted: Tue Mar 05, 2013 11:41 am
by admin
Ambiorix wrote:Can I assume that any declarations in the first case are visible to all the other cases?
Yes.
Ambiorix wrote:
Also, what happens if I declare a variable in one of the later cases? Is this visible from anywhere in the switch, or only in the cases that follow it?
[/quote]
Only the cases that follow. You might want to try it out as well.
Re: About Question enthuware.ocajp.i.v7.2.1119 :
Posted: Mon Dec 30, 2013 4:56 pm
by javaman
java Switcher 1 2 3...
Isn't Switcher called here with 5 arguments: "1"," ","2"," " and "3"??? There are spaces between the numbers.
Re: About Question enthuware.ocajp.i.v7.2.1119 :
Posted: Tue Dec 31, 2013 9:28 pm
by admin
No, spaces are not considered arguments. If you want to give space as an argument, you need to put it in quotes, for example: java Switcher " " 1 2 3 <- here, the first argument is a space.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1119 :
Posted: Tue Jul 01, 2014 9:09 pm
by Shortrope
Here is another oddity:
if case 0 is
not selected, it seems the variable
b in
case 0 is created but
not initialized !
Code: Select all
switch(x) {
case 0 :
boolean b = false;
System.out.println("Case 0\tb = " + b);
break;
case 1 :
b = true;
System.out.println("Case 1\tb = " + b);
break;
}
This code runs fine for either 'case'
But if you comment out
//b = true in case 1, ... you get a compile time error:
Code: Select all
Class.java:19: error: variable b might not have been initialized
System.out.println("Case 1\tb = " + b);
^
You would think if the variable
b was created from the statement in
case 0, it would have been assigned
false as well.
Re: About Question enthuware.ocajp.i.v7.2.1119 :
Posted: Wed Apr 22, 2020 5:30 pm
by codecode
any ideas about this valid observation "You would think if the variable b was created from the statement in case 0, it would have been assigned false as well.", anyone?
Re: About Question enthuware.ocajp.i.v7.2.1119 :
Posted: Thu Apr 23, 2020 3:02 am
by admin
Well, what happened when you printed it out at that point?