Four details of Java switch statement use attention _java

Source: Internet
Author: User
The format of the switch statement is as follows: (its function is to select a piece of code execution)
Copy Code code as follows:

Switch (integer selection factor) {
Case Integer value 1: statement; Break
Case Integer value 2: statement; Break
Case Integer value 3: statement; Break
Case Integer value 4: statement; Break
Case Integer value 5: statement; Break
...
Default: statement;
}

However, the following points should be noted:
The argument type of case in 1.switch can only be of type int, but Byte,short,char type is also possible because Byte,short,shar can be automatically promoted (automatic type conversion) to int, so the final one is the int type. Here's how Java has 8 data types: Byte, short, char, int, long, float, double, and a Boolean in which a Boolean cannot convert to any type of data, and a small type can be automatically converted to a large data type. Large data types are converted to small and must be made stronger.
2.case can be an expression.
3.break is used to jump out of the entire switch statement, and if not, the next branch will be executed.
4. Good programmers make good use of default.

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.