Introduction to Java using NetBeans

Section 5 : Decisions, Decisions

 

The following example demonstrates the general layout of a switch statement.  Notice that this switch statement falls through to the default case:

 

 

 

 

The following switch statement prints the day of the week based on the day number returned by the Calendar get() method.

The Calendar get() method is a method from the Calendar class. This method returns the day of the week (0 for Saturday, 1 for Sunday, and so on) according to local time. Local time is determined by the operating system on which Java is running. Before the Calendar class can be used it must be imported into the package. For example if your project is called DateExample then your package and import line would look as follows:

 

 

If the break statement is not included in a case clause, execution continues on to the next statement within the switch statement. This can be useful when the same set of statements applies to more than one situation:

Or