课程: Java Essential Training: Syntax and Structure

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Switch expressions

Switch expressions

- [Instructor] Switch expressions are similar to switch statements. However, they allow you to directly assign a value when a case is matched. In this example, we're using switch statements to assign a value to the variable message. Switch expressions provide a simpler way to do this. As opposed to declaring message on a standalone line, we could actually initialize it with the result from a switch expression. We can add an equal sign after message and move the switch structure to the right side of the equal sign. Also, we can simplify the cases by replacing :message= with just an arrow, which we type using dash and a greater than sign. This means the same thing, which is, if grade is A for example, a sign, excellent job to the message, variable. Let me do that for the rest of the cases as well. Another nice thing about switch expressions is that they do not need a break statement as there is no fall through like with…

内容