7 data types supported by the "Java" switch case

Source: Internet
Author: User

Tags: init reg CREATE TABLE Mac Enumeration class expression Stat Enumeration

The data type after the switch expression can only be byte,short,char,int four types, enum type and java.lang.String type (only allowed from Java 7), and cannot be a Boolean type.

See a lot of articles on the internet, said switch also support Byte,short,char,int packaging class, first of all can certainly say that switch does not support these packaging classes, but the following code is correct:

     Public Static voidMain (string[] args) {Switch(NewInteger (45)) {         Case40: System.out.println ("40");  Break;  Case45: System.out.println ("45");//will be printed this sentence             Break; default: System.out.println ("?");  Break; }    }

Can print the correct results, after the test byte,short,character,integer next to find all can be printed correctly, and then said switch also support Byte,short,char,int packaging class. This is not entirely true, and the reason that switch can support their wrapper class is because of the automatic unpacking (that is, the automatic conversion of the reference data type to the basic data type), which uses the Jclasslib software to open the above. class file,

10New#2 <java/lang/Integer>to create an integer class object23DUP presses the object's identity onto the top of the stack34 Bipush 45pressing 45 into the stack46 invokespecial #3 <java/lang/Integer.<init>>calling the construction method of an integer type59 Invokervirtual #4 <java/lang/Integer.intValue>Call the Intvalue () method62 Lookupswitch740:40 (+28)845:51 (+39)9DEFALUT:62 (+50)TenGetstatic #5 <java/lang/System.out>get the standard output stream OneLDC #6 <40>Press 40 's index from Chang into the stack AInvokevirtual #7 <java/io/PrintStream.println>Call the println () method -48Goto70 (+22) -Wuyi gestatic #5 <java/lang/System.out> the#8 LDC <45> -Invokevirtual #7 <java/io/PrintStream.println> -59Goto70 (+11) -Getstatic #5 <java/lang/System.out> +LDC #9 <?> -Invokevirtual #7 <java/io/PrintStream.println> +70return

From the 5th line above we can see that the compiler calls the Intvalue () method automatically, if it is using byte automatically calls the Bytevalue () method, if it is short will automatically call the Shortvalue () method, If it is an integer, the Intvalue () method is called automatically. Switch lookup principle is to use key-offset in the target table to find, lookupswitch after the number and Goto after the numbers are regular, for more information can see the java®virtual machine Specification

Therefore , the data type after the switch expression can only be byte,short,char,int four types of shaping, enum type, and java.lang.String type.

7 data types supported by the "Java" switch case

Related Article

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.

Tags Index: