![]() ![]() For the values MONDAY through THURSDAY I print one String for FRIDAY I print a different string and SATURDAY and SUNDAY print their own string. The printTodaysThought method takes one Day value ( theDay), and compares that variable against the constants that are shown in the switch statement. That’s really the only “trick” in this code the rest of it is a standard Java 5 for loop, and it calls the printTodaysThought method once for each constant in the Day enum. This “enum for loop” iterates through all the values in the Day enum, using the values method that comes with Java’s enum type. Inside main I jump right in with this for loop: DiscussionĪs with any Java program, the flow of control starts in the main method. The output is in this order because the enum begins on SUNDAY, and goes in order from there until SATURDAY. When you compile and run this code, the output looks like this: Public static void printTodaysThought(Day theDay)Ĭase THURSDAY: ("Working for the man :)") Ĭase FRIDAY: ("TGIF ") Ĭase SUNDAY: ("Ahh, the weekend. a method that prints a String corresponding to the day value ![]() loop through the enum values, calling the * A Java enum switch statement (switch/case) example. Let’s take a look at the Java source code for my enum example, and then I’ll describe it afterwards: Then in the main portion of the program, I refer to that enum, both in my main method, and in the “print” method that I call from the main method. SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY In this enum/switch example, I first declare an enum type that looks like this: Hopefully this enum/switch example adds a little more complexity to my earlier examples. In this enum tutorial, I want to just focus on using an enum in a switch statement. In my earlier Java enum examples tutorial, I demonstrated how to declare a simple Java enum, and then how to use an enum with a variety of Java constructs, including a Java switch statement, a for loop, and an if/then statement. enum FAQ: Can you share a Java enum switch example, i.e., how to use an enum with a Java switch statement? In the example below, a typical switch statement is written with four possible cases, including default. With the exception of continue, it is possible to use any statement which would cause the abrupt completion of a statement. Abrupt completion of the case statement usually break: This is required to prevent the undesired evaluation of further case statements.default: This is an optional, catch-all expression, should none of the case statements evaluate to true.case: This is the value that is evaluated for equivalence with the argument to the switch statement.There are three critical components to the switch statement: However, unlike if statements, one may not use inequalities each value must be concretely defined. It is used to take the place of long if- else if- else chains, and make them more readable. The switch statement is Java's multi-way branch statement. Visibility (controlling access to members of a class).Using ThreadPoolExecutor in MultiThreaded applications.Using Other Scripting Languages in Java.Splitting a string into fixed length parts.Parallel programming with Fork/Join framework.Java Pitfalls - Threads and Concurrency.Java Pitfalls - Nulls and NullPointerException.Java Editions, Versions, Releases and Distributions.Executor, ExecutorService and Thread pools.AppDynamics and TIBCO BusinessWorks Instrumentation for Easy Integration. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |