Switch in Java jre7 and above supports string implementation details

Source: Internet
Author: User
Tags switch case


The switch in JAVA7 supports the implementation details of string


Author: zsxwing Updated: 2013-03-04 21:08:02 posted: 2012-04-26 13:58:19





Before JAVA7, switch can only support byte, short, char, int, or its corresponding wrapper class and enum type. In Java7, string support, which was called for a long time, was finally added. For example, here is a sample code that uses string in a switch.

1 public class Test {
 2 
 3 public void test (String str) {
 4 switch (str) {
 5 case "abc":
 6 System.out.println ("abc");
 7 break;
 8 case "def":
 9 System.out.println ("def");
10 break;
11 default:
12 System.out.println ("default");
13}
14}
15
16}
 

    In the switch statement, String comparison uses String.equals, so you can use it with confidence. It should be noted that the String variable passed to the switch cannot be null, and the string used in the case clause of the switch cannot be null. Why are these non-null restrictions? In fact, as long as we disassemble this code and see how the bottom layer is implemented, we can understand. The following is the compiled code. Compiled from "Test.java" public class Test extends java.lang.Object {public Test (); Code: 0: aload_0 1: invokespecial # 1; // Method java / lang / Object. "" :() V 4: return public void test (java.lang.String); Code: 0: aload_1 1: astore_2 2: iconst_m1 3: istore_3 4: aload_2 5: invokevirtual # 2; // Method java / lang / String.hashCode :() I 8 : lookupswitch {// 2 96354: 36; 99333: 50; default: 61} 36: aload_2 37: ldc # 3; // String abc 39: invokevirtual # 4; // Method java / lang / String.equals: (Ljava / lang / Object;) Z 42: ifeq 61 45: iconst_0 46: istore_3 47: goto 61 50: aload_2 51: ldc # 5; // String def 53: invokevirtual # 4; // Method java / lang / String.equals : (Ljava / lang / Object;) Z 56: ifeq 61 59: iconst_1 60: istore_3 61: iload_3 62: lookupswitch {// 2 0: 88; 1: 99; default: 110} 88: getstatic # 6; // Field java / lang / System.out: Ljava / io / PrintStream; 91: ldc # 3; // String abc 93: invokevirtual # 7; // Method java / io / PrintStream.println: (Ljava / lang / String;) V 96: goto 118 99: getstatic # 6; // Field java / lang / System.out: Ljava / io / PrintStream; 102: ldc # 5; // String def 104: invokevirtual # 7; // Method java / io / PrintStream.println: (Ljava / lang / String;) V 107: goto 118 110: getstatic # 6; // Field java / lang / System.out : Ljava / io / PrintStream; 113: ldc # 8; // String default 115: invokevirtual # 7; // Method java / io / PrintStream.println: (Ljava / lang / String;) V 118: return} It is estimated that some students Too lazy to look at these assemblies, in fact, the above assembly code written in Java is like the following. As of this writing, everyone should be able to understand why null cannot be used.
 1 public class Test {
 2 public void test (String str) {
 3 int i = -1;
 4 switch (str.hashCode ()) {
 5 case 96354: // "abc" .hashCode ()
 6 if (str.equals ("abc")) {
 7 i = 0;
 8             }
 9 break;
10 case 99333: // "def" .hashCode ()
11 if (str.equals ("def")) {
12 i = 1;
13}
14 break;
15 default:
16 break;
17}
18
19 switch (i) {
20 case 0:
21 System.out.println ("abc");
22 break;
23 case 1:
24 System.out.println ("def");
25 break;
26 default:
27 System.out.println ("default");
28}
29}
30}
 

  If the null passed in by the switch, then calling the hashCode method on a null object at runtime will result in a NullPointerException. If the switch case is written as null, then the hashCode cannot be obtained during compilation, so an error will be reported during compilation. Switch support String is just a syntactic sugar, javac is responsible for generating the corresponding code. The underlying JVM has not been modified on the switch. Refer to http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html If the null passed by the switch, calling the hashCode method on a null object at runtime will result in a NullPointerException. If the switch case is written as null, then the hashCode cannot be obtained during compilation, so an error will be reported during compilation. Switch support String is just a syntactic sugar, javac is responsible for generating the corresponding code. The underlying JVM has not been modified on the switch.
Implementation details of switch support String in Java jre7 and above

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.