Thursday, December 4, 2014

Java 7 feature String in switch

switch-case is a great way to deal with logic. But it has its own limitations. This construct works with integral numbers and characters and enum only. No support for real numbers or Strings. So we are stuck with if-else in those scenarios.

With Java 7, the project team has deleted this gap a little bit, making easier the life of developers. It has added String as the passing parameter to switch-case statements.

Following is an example of this feature,

 /*  
  * Copyright 2014-2015 Palash Kanti Kundu.  
  *  
  * Licensed under the Apache License, Version 2.0 (the "License");  
  * you may not use this file except in compliance with the License.  
  * You may obtain a copy of the License at  
  *  
  *   http://www.apache.org/licenses/LICENSE-2.0  
  *  
  * Unless required by applicable law or agreed to in writing, software  
  * distributed under the License is distributed on an "AS IS" BASIS,  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  * See the License for the specific language governing permissions and  
  * limitations under the License.  
  */  
  
 import java.io.BufferedReader;  
 import java.io.IOException;  
 import java.io.InputStreamReader;  
 /**  
  * @author Palash Kanti Kundu  
  * @version 1.0  
  * @since Mar 5, 2015  
  */  
 public class SwitchTest {  
      /**  
       * @param args  
       * @throws IOException  
       */  
      public static void main(String[] args) throws IOException {  
           String userInput;  
           BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
           System.out.println("Enter a number");  
           userInput = br.readLine();  
           switch (userInput) {  
           case "1":  
                System.out.println("User entered 1");  
                break;  
           case "2":  
                System.out.println("User entered 2");  
                break;  
           case "3":  
                System.out.println("User entered 3");  
                break;  
           default:  
                System.out.println("Invalid user input");  
                break;  
           }  
      }  
 }  

And the output is as follows,

 Enter a number  
 1  
 User entered 1  Enter a number  
 2  
 User entered 2 

With earlier versions the same code would look like following.
 /*  
  * Copyright 2014-2015 Palash Kanti Kundu.  
  *  
  * Licensed under the Apache License, Version 2.0 (the "License");  
  * you may not use this file except in compliance with the License.  
  * You may obtain a copy of the License at  
  *  
  *   http://www.apache.org/licenses/LICENSE-2.0  
  *  
  * Unless required by applicable law or agreed to in writing, software  
  * distributed under the License is distributed on an "AS IS" BASIS,  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  * See the License for the specific language governing permissions and  
  * limitations under the License.  
  */  
  
 import java.io.BufferedReader;  
 import java.io.IOException;  
 import java.io.InputStreamReader;  
 /**  
  * @author Palash Kanti Kundu  
  * @version 1.0  
  * @since Mar 5, 2015  
  */  
 public class SwitchTest {  
      /**  
       * @param args  
       * @throws IOException  
       */  
      public static void main(String[] args) throws IOException {  
           String userInput;  
           BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
           System.out.println("Enter a number");  
           userInput = br.readLine();  
           // Earlier version of Java without support for String in switch  
           if (userInput.equals("1")) {  
                System.out.println("User entered 1");  
           } else if (userInput.equals("2")) {  
                System.out.println("User entered 2");  
           } else if (userInput.equals("3")) {  
                System.out.println("User entered 3");  
           } else {  
                System.out.println("Invalid user input");  
           }  
      }  
 }  

Well, another version of the same program could look like something as follows,

 /*  
  * Copyright 2014-2015 Palash Kanti Kundu.  
  *  
  * Licensed under the Apache License, Version 2.0 (the "License");  
  * you may not use this file except in compliance with the License.  
  * You may obtain a copy of the License at  
  *  
  *   http://www.apache.org/licenses/LICENSE-2.0  
  *  
  * Unless required by applicable law or agreed to in writing, software  
  * distributed under the License is distributed on an "AS IS" BASIS,  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  * See the License for the specific language governing permissions and  
  * limitations under the License.  
  */  
  
 import java.io.BufferedReader;  
 import java.io.IOException;  
 import java.io.InputStreamReader;  
 /**  
  * @author Palash Kanti Kundu  
  * @version 1.0  
  * @since Mar 5, 2015  
  */  
 public class SwitchTest {  
      /**  
       * @param args  
       * @throws IOException  
       */  
      public static void main(String[] args) throws IOException {  
           int userInput;  
           BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
           System.out.println("Enter a number");  
           userInput = Integer.parseInt(br.readLine());  
           // Earlier version of Java without support for String in switch  
           switch (userInput) {  
           case 1:  
                System.out.println("User entered 1");  
                break;  
           case 2:  
                System.out.println("User entered 2");  
                break;  
           case 3:  
                System.out.println("User entered 3");  
                break;  
           default:  
                System.out.println("Invalid user input");  
                break;  
           }  
      }  
 }  


Now this one is a great feature Java 7 introduced to deal with Strings in a more fashionable and easier way.

Hope you enjoyed reading this article. If you found  it to be useful, I would like to reshare this article in your networks as well...
Prev    Next

Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu 

No comments:

Post a Comment