Switch case statement processing string in java

switch can be used to determine the string in java

Source code:

package com.HePing.sturct;

import java.util.Scanner;

public class Switch_Demo02 {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();

        switch (name){
    
    
            case "徐和平" :
                System.out.println("徐和平");
                break;
            case "和平" :
                System.out.println("和平");
                break;
            default:
                System.out.println("弄啥嘞");

        }
    }
}

After decompiling the source code:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.HePing.sturct;

import java.util.Scanner;

public class Switch_Demo02 {
    
    
    public Switch_Demo02() {
    
    
    }

    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        byte var4 = -1;
        switch(name.hashCode()) {
    
    
        case 695143:
            if (name.equals("和平")) {
    
    
                var4 = 1;
            }
            break;
        case 24205047:
            if (name.equals("徐和平")) {
    
    
                var4 = 0;
            }
        }

        switch(var4) {
    
    
        case 0:
            System.out.println("徐和平");
            break;
        case 1:
            System.out.println("和平");
            break;
        default:
            System.out.println("弄啥嘞");
        }

    }
}

The string is actually judged by the value of hashcode

Guess you like

Origin blog.csdn.net/qq_45361567/article/details/112665658