[One knowledge point]18-java language per day-accepts string types in switch

Source: Internet
Author: User

With the addition of a new feature in Java7, switch can accept variables of type string, which saves the complexity of writing large if ("xxx". Equals (YYY)). The sample code is exempt, just a change of data type, if you will use the original switch and in 5 minutes to make a string type of switch, it is recommended to abandon the programmer it ...

As for the internal implementation, Java does not add new content, in fact, the code is converted into this way

Switch (Xxx.hashcode ()) {case 1024x768:  //Assume the original is "AAA", while "AAA" Hashcode is 1024.    if ("AAA". Equals (XXX)) {        //Here is the original "AAA" under the Code    }case ...}

Of course, this is only a specific version of the implementation, not guaranteed in the future will always be this way.

Finally, 2 questions are left:

1. Why should the IF ("AAA". Equals (XXX)) This judgment?

2. Ask yourself if you write this code yourself, will you write this judgment in the first time?

[One knowledge point]18-java language per day-accepts string types in switch

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.