Transcript from the "Switch Expressions" Lesson
>> So there's also switch expressions. So these are similar to the switch statements, but they allow you to directly assign a value to a variable. All right, so we're going to copy that code we just wrote will copy it into a new class. And we're gonna use a switch expression to print the message versus a statement so you can see the difference, okay.
[00:00:26] So let's go back to our ID. And if we go over to the project let's right-click on grade message and choose copy and then on decision structure paste and they'll ask you to rename this because you can't have two classes with the same name. So we'll just do _switchexpressions All right.
[00:01:15] So right here where we have string message what we're doing in this which structure is where assigning a value to message well You can do all of this with an expression expressions are specifically switch expressions were specifically designed for this sort of switch statement, right. This is like a lot of bulk in order to just assign something.
[00:01:43] So instead I can say string message equals and then on the right hand side of this statement, I would use the switch. So I can say I can just move this switch up to the right hand side. So I'm gonna switch on grade, I still have my curly braces and all of that.
[00:02:04] The way that the cases are written changes a bit. So it's a bit more streamlined, instead of this :, after the case, I use a- and an >, a caret, right caret, to make an arrow. And then I put the mess, whatever I want the message to be.
[00:02:25] So I'm gonna erase message=, and we'll just put Excellent job. You don't need break statements with switch expressions because there is no fall through, all right? So this is not even needed, all right? So let's go ahead and clean all the rest of these up. Notice how streamlined this is.
>> Do you still have to initialize it?
>> Let's see. All right, we're gonna write the default one and then I'll comment it out. All right, and then we just end this with a semicolon. Much more streamlined, right? All right, so let's comment out default. And it looks like you don't need the default and you don't need to do some special initialization of it.
>> So the switch will always return.
>> It has to return something. You want to see what happens if we put z I do all right let's print this out. I'm gonna run this without the default case. Okay, so great. It does not have a compile error meaning it didn't complain before we run it, but there's a runtime error that says.
[00:04:18] The switch expression does not cover all possible values. So, we did in fact get an error, but at runtime. So, you do need the default. [LAUGH] Great question, any other questions? Let's run it and with a good case. So enter our letter grade, let's say C. And yep, good job.
[00:04:50] Okay, so that works. Now if you want it the effects of maybe follow through or you basically just had two cases with the same code that you wanted to execute. You don't want to like ever just kind of be repeating stuff like for example something like that, that's not good.
[00:05:13] Notice they even have a little squiggly line, like, what are you doing here? All right, so if I had, like if I wanted to say the same thing for two cases, I would simply do a comma delimited list, and for both of those cases, it would print that.
[00:05:31] Let's say that you wanted to execute multiple statements because this one little arrow is really cute but what if you had to do four or five statements? You could probably just add curly braces and then you can write whatever you want to write. It needs to be statements, but your final line a lot of that has to be yield.
[00:05:57] You have to yield back some value because remember the whole purpose of the switch expressions is to assign some value to something. So maybe you need to do some sort of calculation to get that value. But your last statement must be a yield. Great. Okay, last but not least, switch expressions they're not over placement for, switch statements, basically there's cases for both.
[00:06:30] For statements, you would want to use those when you essentially want to execute code. It's not a matter of assignment. You just need to execute something based on a condition versus a switch expression is when you need to conditionally assign a value to a variable. Okay. All right, questions.
>> Are you seeing so before us the yield you can have different statements?
>> Yes. All right, so let's say we may want to print something out, print. And you can do whatever you want, right? You can have however many statements that final line has to be a yield.
[00:07:14] Is that used anywhere outside of a case or a switch statement?
>> That arrow in lambda expressions.
>> So it's relatively new to the Java language, the use of the arrow, starting I think like Java 8-ish. But yeah it's used for these and switch expressions is like kind of brand new to the language maybe a year and it's used in lambda expressions.