• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

switch expressions - java 17

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there, I've been taking some mock questions re Java 17.

I noticed this snippet:



Where it says //redundant but allowed, shouldn't there be a yield statement since its using {} ? Or is it that because it isn't returning a value, this is permitted?

Thanks
 
Master Rancher
Posts: 5060
81
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right, if it were "returning" a value, it would have to use yield.  Since it's not, break is fine (though pointless here).
 
Tim Bant
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cheers - I wonder if this could be one of those, however, whereby they changed the constraint slightly in later versions, for example, this page here (https://docs.oracle.com/en/java/javase/17/language/switch-expressions-and-statements.html#GUID-BA4F63E3-4823-43C6-A5F3-BAA4A2EF3ADC) under Exhaustiveness, you'll see it says it must have a yield when using {}
 
Mike Simmons
Master Rancher
Posts: 5060
81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, we're mixing up switch statements and switch expressions.  Your first example was actually a switch statement, with no "return" values.  There, break is fine, and yield does not work.  When I said "if  it were 'returning' a value", I was actually talking about switch expressions.  This is also what they're talking about in the documentation you're now looking at.  Once you place a switch in a context where it's expected to have a value (e.g., putting it to the right of an equals sign, or as part of a larger expression) then the rules for switch expressions kick in, and break is no longer allowed, only yield.
 
Tim Bant
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Makes sense, thanks for clarifying for me!
 
MyExamCloud Software Support
Posts: 753
3
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Differences Between the Switch Statement and Switch Expression

- Expression Result: The switch statement does not return a value, while the switch expression returns a value.
- Syntax: The switch statement uses the colon (`:`) notation, while the switch expression uses the arrow (`->`) notation.
- `break` Statement: The `break` statement is required in each `case` block in the switch statement, while it is optional in the switch expression.
- `default` Block: The `default` block is required in the switch statement, while it is optional in the switch expression.
- Fall-Through: Fall-through is not allowed in the switch expression, while it is allowed in the switch statement.
- Multiple Constants: Multiple `case` constants can be in a single `case` label in the switch statement, while the switch expression does not allow this.
- Data Types: The switch statement allows primitive data types, wrapper classes, Enums, and Strings as `selector_expression`, while the switch expression only supports Enums and Strings.
- Scope: The variables defined in each `case` block in the switch expression are scoped to that block only, while they are accessible in the entire `switch` statement in the switch statement.

Read this article
 
Don't MAKE me come back there with this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic