Switch in Java

Source: Internet
Author: User
Tags case statement

A switch statement consists of a control expression and multiple case labels.

The types supported by the switch control expressions are byte, short, char, int, enum (Java 5), String (Java 7).

The Switch-case statement can be fully interoperable with the If-else statement, but generally, theswitch-case statement executes more efficiently .

Default is executed when the current switch cannot find a matching case. Default is not a must.

Once the case is matched, the subsequent program code is executed sequentially, regardless of whether the following case matches until a break is encountered.

Usage examples:

1). int type Switch Example

inti = 3;

switch (i) { case 1 :      System.out.println( 1 );      break ; case 2 :      System.out.println( 2 );      break ; case 3 :      System.out.println( 3 );      break ; default :      System.out.println( 0 ); }2). Enum (enum) type switch example public class TestSwitch {      static enum E {          A, B, C, D      }      public static void main(String args[]) {          E e = E.B;          switch (e) {          case A:              System.out.println( "A" );              break ;          case B:              System.out.println( "B" );              break ;          case C:              System.out.println( "C" );              break ;          case D:              System.out.println( "D" );              break ;          default :              System.out.println( 0 );          }      }3). String Type Switch Example

String str = "C";

switch (str) { case "A" :      System.out.println( "A" );      break ; case "B" :      System.out.println( "B" );      break ; case "C" :      System.out.println( "C" );      break ; default :      System.out.println( 0 ); }4). Break Trap: Break is used in switch to end the current process. Once the case is matched, the subsequent program code is executed sequentially, regardless of whether the following case matches until a break is encountered. int i =  2 ; switch (i) { case 1 :      System.out.println( 1 ); case 2 :      System.out.println( 2 ); case 3 :      System.out.println( 3 ); default :      System.out.println( 0 ); }Output Result: 2 3 05).Default:default is executed when the current switch cannot find a matching case. Default is not a must. int x =  0 ; switch (x) { case 1 :      System.out.println( 1 ); case 2 :      System.out.println( 2 ); default :      System.out.println( "default" ); } Precautions1). switch (a), the value of a in parentheses can only be an integer or a numeric type that can be converted to an integral type, such as Byte, short, int, char, and enumeration; It is important to emphasize that long and string types are not java7 before the switch statement. After Java7, string can be used. 2). Case B:c;case is a constant expression, which means that the value of B can only be constant or int, byte, short, char (for example, 1, 2, 3, 200000000000 (note that this is an integer)), If you need to write an expression or a variable here, add a single quotation mark; The statement after the case can be used without curly braces, or C does not have to be wrapped in curly braces;

3). The case must be followed by a constant, and cannot be a long or float type:

private int click_query = 1;

Switch (TAG) {case  click_query:// error, Click_query is not a constant , final decoration, must be a final type constant, not a final variable, for example: final int Click_query = 1; But it cannot be final int click_query;
Query (); Break

4). The range of values following the case:

byte a = one;  Switch (a) {//C case  11:system.out.println ("11"); break;  225 The range of byte is exceeded (the range of byte is -127~128), so an error occurs

Switch in Java

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.