Switch usage in java and modify witch usage

Source: Internet
Author: User

Switch usage in java and modify witch usage

The switch keyword is no stranger to most java learners. Because the written examination and interview often ask about its usage, here is a simple summary:

  • The types that can be used for switch judgment include byte, short, int, char (JDK1.6), and enumeration type. However, after JDK1.7, the String type judgment is added.
  • If break is rarely written in the case statement, no error will be reported during compilation, but the statements under all the case conditions will not be judged until the default statement is executed.
  • If there is no qualified case, execute the code block under the default statement. The default statement is not required or can be left empty.
1 package codeAnal; 2 3 public class SwitchDemo {4 5 public static void main (String [] args) {6 stringTest (); 7 breakTest (); 8 defautTest (); 9} 10 11/* 12 * default is not required, or you can leave 13 * output: case two14 */15 private static void defautTest () {16 char ch = 'a '; 17 switch (ch) {18 case 'B': 19 System. out. println ("case one"); 20 break; 21 case 'A': 22 System. out. println ("case two"); 23 break; 24 case 'C': 25 System. ou T. println ("case three"); 26 break; 27} 28} 29 30/* 31 * The break is rarely written in the case statement, no 32 * errors will be reported during compilation, but the statements under all the case conditions will be executed until the default statement 33 * is output in the following code: case two34 * case three35 */36 private static void breakTest () {37 char ch = 'a'; 38 switch (ch) {39 case 'B': 40 System. out. println ("case one"); 41 42 case 'A': 43 System. out. println ("case two"); 44 45 case 'C': 46 System. out. println ("case three"); 47 default: 48 break; 4 9} 50} 51 52/* 53 * switch is used to judge String type 54 * output: It's OK! 55 */56 private static void stringTest () {57 String string = new String ("hello"); 58 switch (string) {59 case "hello": 60 System. out. println ("It's OK! "); 61 break; 62 63 default: 64 System. out. println (" ERROR! "); 65 break; 66} 67} 68}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.