Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis of the underlying principle of JAVA string type switch

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains the "JAVA string type switch underlying principle analysis", the article explains the content is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "JAVA string type switch underlying principle analysis" bar!

Preface

Switch sentence is a very basic knowledge, it is not difficult to master, and the grammar is relatively simple. But most people basically know it and don't know why. For example, early JDK only allowed the value of switch expression int and basic types below int type, but later JDK allowed matching comparison string and enumerated types. How did this happen? What is the principle? This article will explore in depth.

Basics

The version of Java we use now basically supports the String type. Of course, in addition to the String type, int, char, byte, short, enum and so on are also supported. However, in the bottom implementation, it is based on integers, that is, int, byte, short and so on.

Let's first look at a simple example of int, the main part of the source code

Public static void main (String [] args) {int n = 2; switch (n) {case 1: break; case 2: break; case 3: break; default:}}

Then compile with the javac command, and the javap command is decompiled to get the following key parts of the bytecode:

0: iconst_2 1: istore_1 2: iload_1 3: tableswitch {/ / 1 to 31: 28 2: 31 3: 34 default: 37} 28: goto 37 31: goto 37 34: goto 37 37: return

If you don't understand, you can click here to see the reference table.

Of course, if we don't bother to read it, we can decompile the class file directly into the source code. We can directly drag the class file into IDEA to get the following code:

Public static void main (String [] var0) {byte var1 = 2; switch (var1) {case 1: case 2: case 3: default:}}

In general, it doesn't change much from the source code, except that all int types are converted to byte types. The reason for the conversion here is that the value in our original case is just within the range of byte. If the value of case is slightly higher, it may be converted to the short type, and if it is larger, it will be directly the int type. It should be noted that float and long are not supported in switch.

String type explanation

With the above understanding, the following should be much easier.

Again, let's start with the source code.

Public static void main (String [] args) {String str = "sdf"; switch (str) {case "aaa": break; case "ccc": break; case "bbb": break; default:}}

Then it is compiled and then decompiled into IDEA to get the decompiled code

Public static void main (String [] var0) {String var1 = "sdf"; byte var3 =-1; switch (var1.hashCode ()) {case 96321: if (var1.equals ("aaa")) {var3 = 0;} break; case 97314: if (var1.equals ("bbb")) {var3 = 2;} break; case 98307: if (var1.equals ("ccc")) {var3 = 1;}} switch (var3) {case 1: case 0: case 2: default:}

As you can see, the switch of type String is converted to a hash comparison of strings, and its hash returns the type int. In the same case of hash, the value of the string is compared by the equals method, so it is necessary to introduce the local variable var3.

Thank you for reading, the above is the "JAVA string type switch underlying principle parsing" content, after the study of this article, I believe you on the JAVA string type switch underlying principle parsing this problem has a deeper understanding, the specific use of the need for you to practice and verify. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report