Usage and considerations for using Switch-case in Java Super full summary

Source: Internet
Author: User

http://m.blog.csdn.net/blog/tianyazaiheruan/8988420

Today qnovations teacher in the use of switch, this design to the most basic content, may forget some of its basic grammar, there are some errors, so impromptu from a variety of data query summed up the following content, hoping to help those who are plagued with switch errors and various details of the problem of friends!

1.switch-case Precautions:

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 do not work on the switch statement.

Case B:c;case is a constant expression, that is, the value of B can only be a constant (you need to define a final type of constant, which is explained in detail later) or int, Byte, short, char (such as 1, 2, 3, 200000000000 (Note that this is an integer)), if you need to write an expression or a variable here, then add a single quotation mark; The statement after the case can be used without curly braces, that is, C does not need braces wrapped;

Default is to execute a case without a match, and default is not required.

2. Case study:

1. Standard type (the break statement is followed by case, the value after the case is an integer)

int i=3; switch (i) {case 1:system.out.println (1), break, Case 2:system.out.println (2);  


2. Constant type (the break statement follows the case, and the value after the case is constant)

Private final int num1=1;private final int num2=1;int i=3; switch (i) {case NUM1:System.out.println (1), break, Case NUM2:System.out.println (2);  


3. Expression type (the break statement is followed by case, the value after the case is an expression)

int i=3; int B = 2;switch (i) {case ' class name. GetId () ': System.out.println (1), break, case ' B ' System.out.println (2);  DEFAULT:SYSTEM.OUT.PRINTLN ("Default"); Break }


3. Error analysis when using switch:

1). The second situation is prone to error:

Discover Problems

private int click_query = 1;private int click_reset = 2; @Overridepublic void OnClick (View v) {int tag = (Integer) v.gettag (); Switch (TAG) {case  click_query:   QUERY ();  break;  Case Click_reset:   RESET ();  Break }}


Compile-time error: Click_query and click_reset--case expressions must be constant expressions

Solve the problem

The case must be followed by a constant, which must be constant, declaring the above two variables final.

Private final int click_query = 1;

Private final int click_reset = 2;

2). The following is a simple notation for switch:

Switch (A) {

Case B;

}

The value in part A must be of type int, or an expression that can be automatically converted to int type. That is, part A can be a byte\short\char\int type (because these types can be automatically converted to int type).

The second thing to emphasize is that the value of part B in the program must be a single byte\short\char\int type or a final type variable.

But the final type of variable is also required, that is, it must be a compile-time constant, how to say, see the following program section:

Final int a = 0;

final int b;

The second statement is a variable that cannot be recognized at compile time because it is not initialized, and of course, this statement is also wrong. So the value after the summary case can be a constant value or a final type. Then look at the following program section:

public class Testswitch {public static void main (string[] args) {  byte a = one;  Switch (a) {//C case  11:system.out.println ("11"); break;  Case 225:system.out.println ("11"); break;//D  }}}


Is the code correct? The answer is in the negative. Although it is legal in C, that is, a value of byte can appear in switch, but the statement at D is the second case after the value is 225 size exceeds the range of byte, so it is wrong. Then the value of the case cannot be duplicated. So be aware of it in use.

3. Forgetting to write a break error

And then in the use of switch-case is the most easily overlooked is to forget in each case after processing after forgetting to write a break; What is the result of it, and the following small program will tell you:

public class Testswitchcase {public static void main (string[] args) {  byte a = 2;  Switch (a) {case  1:system.out.println ("a");  Case 2:system.out.println ("B");  Case 3:system.out.println ("C");  Case 4:system.out.println ("D");  DEFAULT:SYSTEM.OUT.PRINTLN ("Default");}}}  ========= output is: B  C  D  Default--------------------------


Did you see it? Even the default is executed, note the Terminator break; ok.

Reprint Please specify: This article from Yangkai Exclusive channel

Usage and considerations for using Switch-case in Java Super full summary

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.