The switch in Java jre7 and later versions supports the implementation details of String, jre7switch

Source: Internet
Author: User
Tags switch case

The switch in Java jre7 and later versions supports the implementation details of String, jre7switch
The switch in Java 7 supports String implementation details.

Author: zsxwing updated: 21:08:02 release: 13:58:19

 

Before Java 7, the switch can only support byte, short, char, int, corresponding encapsulation class, and Enum type. In Java 7, String support that has been called for a long time has finally been added. For example, the following is a sample code for using 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 uses String. equals for comparison, so you can safely use it. Note 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 required? In fact, we only need to decompile this code to see how the underlying layer is implemented. 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: ld C #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/Sys Tem. 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/Pri NtStream; 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 are too lazy to read the Assembly. In fact, the above assembly code is written in Java as follows. Here, we 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 switch is passed in null, NullPointerException will occur when a hashCode method is called for a null object at runtime. If the switch case is null, The hashCode cannot be obtained during compilation. Therefore, an error is reported during compilation. The switch supports String as a syntactic sugar, and javac is responsible for generating the corresponding code. The underlying JVM is not modified on the switch. Refer to the http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html if the switch passes in null, then calling the hashCode Method for a null object at runtime will produce NullPointerException. If the switch case is null, The hashCode cannot be obtained during compilation. Therefore, an error is reported during compilation. The switch supports String as a syntactic sugar, and javac is responsible for generating the corresponding code. The underlying JVM is not modified on the switch.
Java switch statement condition: Use string to determine whether it can be converted to int or char

If your str is a numeric type, you can use Integer. parseInt (str) to convert str to a numeric type. However, if your str is a character, you need to convert it into a character.

Char [] ch = str. toCharArray ();
Switch (ch [0])

However, only one character is required for the string to be used in this way. If there are multiple characters, it will not work.

In java, how does one define a String type variable switch () to receive this variable?

It seems that it is true that, starting with jdk 1.7, the switch statement supports the String type parameters. In addition, it also supports int, char, byte (and its packaging class Integer, Byte, Character) and enum types. Switch in jdk 1.6 does not support the String type. Only the following types are supported. Therefore, in jdk 1.6 and earlier versions, you can define an enum class to store your String.

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.

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.