String in the 1.3.1 switch statement

Source: Internet
Author: User

Switch statement is a kind of efficient multi-path statement, can save a lot of complicated nested if judgment;

In Java 6 and before, the constants in a case statement can only be byte, char, short, and int (or the corresponding encapsulation class) or enumeration constants, and string is added to the Java 7 specification, after all it is a constant type;

Demo:

 Public classcoinswitchstring { Public Static voidMain (string[] args) {Printday ("Sunday"); Printday ("Tuesday"); Printday ("Ten"); }     Public Static voidPrintday (String dayOfWeek) {Switch(dayOfWeek) { Case"Sunday": System.out.println ("Sunday"); Break;  Case"Monday": System.out.println ("Monday"); Break;  Case"Tuesday": System.out.println ("Tuesday"); Break;  Case"Wednesday": System.out.println ("Wednesday"); Break;  Case"Thursday": System.out.println ("Thursday"); Break;  Case"Friday": System.out.println ("Friday"); Break;  Case"Saturday": System.out.println ("Saturday"); Break; default: System.out.println ("Error: '" + DayOfWeek + "is not a day of the week!"); }    }    }

Ran as Java application:

' Ten ' isn't a day of the week!

String in the 1.3.1 switch statement

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.