Type of the Switch Parameter in Java

Source: Internet
Author: User

The switch in Java does not have the convenience of C #. in C #, string can be used. Java and C ++ can only use integer types.

From: http://hi.baidu.com/%CE%BA%DE%A8/blog/item/85eaa10376c74c7d3912bbb4.html

Only int-type values can be placed in the brackets behind the switch.,Note that only the int type can be put, but the byte, short, and Char Types can also be put, because byte, short, and Shar can be automatically upgraded (automatically converted) to intYou can put them in another way. To put it bluntly, the byte, short, and Shar types you put will be automatically converted to the int type (extended, automatically converted, and secure ), in fact, the last put is the int type!

The principle is as follows: in Java, eight basic data types are involved in conversion. The boolean type cannot be converted to the boolean type, and the boolean type cannot be converted to other types, so the remaining 7 types are based on the number range of their tables (that is, the maximum number size that can be expressed, for example, char is 0 to 65535, byte is-128 to positive 127) sort byte, short, Char, Int, long, float, and double.

Rules:

1. small to large conversion (width), automatic conversion, sometimes it will be automatically upgraded to a large type, such as switch

2. Large to small conversions (narrowing) must be forced to convert the type, so long cannot work. If you want to do so, you have to convert it to (INT) Long. Similarly, float and double do not work. If you want to do so, you can turn it upside down.

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.