String of support for the switch

Java 7 in, switch the parameter can be of type String, this is a very convenient improvement for us. So far switch supports several types of data: byte short int char String. However, as a programmer we must not only know how easy he has, but also know how it is implemented, the switch supports integer is how to achieve it ? Character is how to achieve it? String type it? A bit of Java development experience people will guess at this time is switched support to the string equals () method and the hash code () method used. So in the end is not the way to do both? Next we will look at the switch in the end is how to achieve.

A switch for integer achieved support

Here is a very simple Java code, a definition of a variable INT, then the switch statement is determined. Execute the code content output is 5, then we will decompile the code below, to see him in the end is how to achieve.

public class switchDemoInt {
    public static void main(String[] args) {
        int a = 5;
        switch (a) {
        case 1:
            System.out.println(1);
            break;
        case 5:
            System.out.println(5);
            break;
        default:
            break;
        }
    }
}
//output 5

The decompiled code is as follows:

public class switchDemoInt
{
    public switchDemoInt()
    {
    }
    public static void main(String args[])
    {
        int a = 5;
        switch(a)
        {
        case 1: // '\001'
            System.out.println(1);
            break;

        case 5: // '\005'
            System.out.println(5);
            break;
        }
    }
}

We found that the decompiled code and code comparison prior to addition to the two multi-line comments without any distinction, then we know that switching is a value judgment on the int direct comparison of integers .

Second, to achieve the switch character support

Directly on the code:

public class switchDemoInt {
    public static void main(String[] args) {
        char a = 'b';
        switch (a) {
        case 'a':
            System.out.println('a');
            break;
        case 'b':
            System.out.println('b');
            break;
        default:
            break;
        }
    }
}

Compiled code as follows: `public class switchDemoChar

public class switchDemoChar
{
    public switchDemoChar()
    {
    }
    public static void main(String args[])
    {
        char a = 'b';
        switch(a)
        {
        case 97: // 'a'
            System.out.println('a');
            break;
        case 98: // 'b'
            System.out.println('b');
            break;
        }
  }
}

By comparing the above code we found that: when a character type of comparison, the comparison is actually the ASCII code, the compiler will char variables into corresponding INT variable

Third, to achieve the switch string support

Or the first on the code:

public class switchDemoString {
    public static void main(String[] args) {
        String str = "world";
        switch (str) {
        case "hello":
            System.out.println("hello");
            break;
        case "world":
            System.out.println("world");
            break;
        default:
            break;
        }
    }
}

Decompile the code:

public class switchDemoString
{
    public switchDemoString()
    {
    }
    public static void main(String args[])
    {
        String str = "world";
        String s;
        switch((s = str).hashCode())
        {
        default:
            break;
        case 99162322:
            if(s.equals("hello"))
                System.out.println("hello");
            break;
        case 113318802:
            if(s.equals("world"))
                System.out.println("world");
            break;
        }
    }
}

See this code, you know the original string by switching equals()and hashCode()methods to achieve. Remember, the switch can only use integer , for examplebyte . short, char(Ackii code is an integer) as well int. Fortunately hashCode()method returns int, instead long, this is easy to remember by hashCodethe English view of reports of intthis fact. A closer look can be found, perform switchthe actual English hash value, and then by using the method is relatively equal to a security check, the check is necessary because the hash collision may occur. Thus its performance is not as good for use enumeration handover or pure integer constants, but this is not bad. Because the Java compiler only adds a equalsmethod, if you compare a string literal, then it will be very fast , such as "abc" == "abc". If youhashCode() call the method are also taken into account, then no amount of time will call overhead, because once the string is created, it will cache the hash value. So if this switchstatement is in English with a loop, such as a value item by item processing, recycling or game engine to render the screen, here hashCode()call overhead method actually not  be very large.

Well, that's a switch on implementation support for integer, character, and string, and summarize what we can find, in fact, to switch supports only one data type, that is, integer, other data types are converted into after using the switch integer.

 

Guess you like

Origin www.cnblogs.com/lujiahua/p/11408736.html