Join David Gassner for an in-depth discussion in this video Using strings in switch statements, part of Java Advanced Training.
The switch statement lets you evaluate a variable and then branch your code depending on the variable's value. The switch statement has always been available in Java. But up through Java 6, you could only evaluate primitive variables, that is floats, longs, booleans, integers, and so on. In Java7, you can use switch to evaluate a string. Now, I showed this in the Java Essential Training course. But in this course, we're working with Java 7 and so I'll be able to do a demonstration that everybody can follow along.
For this demonstration I'll use a project from the exercise files named StringsInSwitch. It has two classes, StringsInSwitch, the Main class, and Olive. In the Main class, I'm declaring three instances of the Olive class, and I am calling a constructor method, and passing in the name, and the olive's color. The Olive class has these two public fields, oliveName and color, and the oliveName field is a string, and that's what I'll be evaluating with a switch statement. Going back to the Main class, I'll place the cursor at the end of the code within the Main method.
I've created an array list that's storing references to each of the three olives, and I'm going to create a random integer value from 0 to 2, which allows me to pick one olive at random. For this purpose, I'll use a class named random. It's a member of the java.util package, and it's a part of the core Java Developer's kit. I'll name this variable generator, and I will instantiate it using the Random class' constructor method. Next, I'll generate my random integer.
I'll declare the variable, and I'll name it index. I'll get its value by calling a method of the generator object named nextint. The random class supports methods for generating a random integer, a long integer, booleans, doubles, floats, and other data types. I'll use a version of the nextint method, which accepts an integer argument, and I will pass in a value of 3. This determines the maximum value of the random number that's returned. And with integers, you pass in a value that's 1 greater than the maximum you want.
So, if I pass in 3, I'll get back a number that's either 0, 1, or 2. If I were working with a deck of playing cards, I'd pass in 52, and then my lowest possible value would again be 0, but the highest possible value would be 51. Now, to show that this is working, I'll do a little bit of system output, and I will output the string a random value, and then I'll append to that the index variable. I'll save and run the code, and each time I run the code, I'll get back a randomized value from 0 to 2.
When you're running the code on your computer, your results might vary because the randomizing happens each time the application is run. I'll return to my code, and now, I'll use that index variable to get a reference to one of the olives in the list. I'll declare a new olive variable named o, and I'll call list.get, and I'll pass in the index value. So now, I have a randomized reference to one of the olives, but I don't know which one will be returned each time I run the code. Now, it's time for the switch statement.
On the next line, I'll type in the word switch, in all lowercase, and I'll press Ctrl+Space to choose the switch code template. That creates the construct of the switch statement and lands the cursor on the key value within the parentheses, and I'll replace key with o.oliveName. That's the string field that represents the olive's name. For my first case, I'll use Kalamata. If the oliveName has that value, I'll do a little bit of system output, and I'll output the string "It's Greek!" Now, I'll copy that initial case, and I'll paste it in a couple of times.
And for the second two cases, I'll replace the case value and the nationality of the Olive, a Picholine is French, and a Ligurio is Italian. Then I'll come down to the default case, and I'll output the string "I don't know where it's from!" I save my changes, and run the code. And the first time, I get olive 1, and it's French, the second time it's Italian, the third time it's Italian again, and I'll keep on running the code and show that each time I get back another olive.
So, in this demonstration, you've seen how to use the random class, something that wasn't really a part of this particular lesson, but is useful to know. But more importantly, you've seen that you can now use the switch statement to evaluate string-based variables, something you weren't able to do prior to Java 7.
- Installing Java on Windows and Mac OS X
- Installing Eclipse
- Using new features such as simplified generics
- Working with advanced class structures (member, local inner, etc.)
- Using the Reflection API
- Navigating inheritance trees
- Managing unordered and ordered sets
- Peeking and polling with queues
- Testing and error handling
- Managing files and directories
- Working with I/O streams
- Next steps with Java