Java Programming Ideas (fourth Edition) Learning notes----4.8 switch (knowledge points updated)

Source: Internet
Author: User

The switch statement differs from the If-else statement in that the switch statement can have multiple possible execution paths. In the fourth edition of Java programming idea, the syntax format of the switch statement is described:

Switch (integral-selector) {        case integral-value1:             statement;                Break ;          Case integral-value12:            statement;               Break ;         default :            statement;        }

where Integral-selector (integer selection factor) is an expression that can produce an integer value. It also indicates that the selection factor must be an integer value of int or char, or an enum enum type. Since the fourth edition of Java programming thought was written on the basis of JDK1.5, it is inevitable that there will be some obsolescence in the present book.

In the description of the switch statement found in the Java official manual, a switch works with the byte, short, char, and int primitive data types has been explicitly identified. It also works with enumerated types, the String class, and a few special classes that wrap certain primitive Types:chara Cter, Byte, short, and Integer. where enum enum types are new additions in JDK1.5, and switch supports string strings after JDK7.

When programming using the JDK7 version of the environment, the selection factor of the switch statement is used, and if the specification is not met (such as the float type variable), a hint is generated: cannot switch on a value of type float . Only convertible int values, strings or enum variables is permitted because, Byte,short,char can be implicitly converted to I NT so the INT type variable also contains the Byte,short,char type variable.

Summarize:

the data types that can be used as switch selection factors are:

    • Char,byte,short,int and their packaging class Character,byte,short,integer
    • ENMU enumeration (since jdk1.5)
    • String string (since jdk1.7)

Java Programming Ideas (fourth Edition) Learning notes----4.8 switch (knowledge points updated)

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.