public void switchString(String input){
switch(input){
case "a" : System.out.println( "apple" );
case "b" : System.out.println( "bat" );
break;
case "B" : System.out.println( "big bat" );
default : System.out.println( "none" );
}
}
public static void main(String[] args) throws Exception {
TestClasstc = new TestClass();
tc.switchString("B");
}
hi according to eclipse switch cannot evaluate string so this won't compile....
About Question enthuware.ocajp.i.v7.2.860 :
Moderator: admin
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.860 :
Please try using JDK 1.7.
HTH,
Paul.
HTH,
Paul.
-
- Posts: 3
- Joined: Mon Dec 07, 2020 1:34 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.860 :
I thought the switch variable needs to be a compile time constant, and not passed through as parameters to functions according to Selikoff and Boyarsky book.
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.860 :
No, switch case labels must be constants, not the switch variable.
Who is online
Users browsing this forum: No registered users and 5 guests