• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

errata : OCP Java SE17 Developer Study Guide P116 - structure of a switch expression (Sybex CSG 17)

 
Greenhorn
Posts: 5
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A previous errata correction states that a semicolon must be placed at the end of the code in Figure 3.4 (which shows the structure of a switch expression).
This is only true if the Optional assignment (int result =) is coded.
Without the Optional assignment, code compiles and works fine without the semicolon at the end of the curly brace - at least it does so under java 17 for me.
Note also that the default branch is required only for the switch statement that includes the Optional assignment when all cases are covered.
 
Bartender
Posts: 15720
367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm afraid you're mistaken. The code inside printSeason() is not a switch expression, but rather a switch statement.

When you assign the return value of the switch to a variable, that's a hint to the compiler that the switch is an expression and not a statement. And in the case of a switch expression (as in printSeason2()), you need a default case and a semicolon at the end.
 
Dave Malcolm
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm only relating what it says in the (very confusing then) book on P117 in the section on switch expressions.

So the -> can be used in ordinary switch statements then can it?
 
Saloon Keeper
Posts: 3931
43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just 2 cents to add to Stephan's answer:

The constraint for switch expression, when for any input value it must return a value OR throw an expression, is called exhaustiveness.  More details here.

The assignment is not optional for switch expression (i.e. when you return something from case blocks), switch expression always must be on the right side of the assignment.

Best regards,
MZ
 
Dave Malcolm
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well this bloody book is very badly written then - totally confusing. Take a look at the relevant section on P117 and tell me it's not confusing?

It specifically says - and i quote directly...

"Let's rewrite our printSeason() method from earlier using a switch expression:
   public  void printSeason(int month) {
       switch(month) {
           case 1, 2, 3 -> System.out.println("Winter");
           case 4, 5, 6 -> System.out.println("Spring");
           case 7, 8, 9 -> System.out.println("Summer");
           case 10, 11, 12 -> System.out.println("Fall");
       }
"

that's a direct quote from the book. I repeat that if what you're both saying is true then the book is awful because this is supposed to prepare people for the exam.
 
Mikalai Zaikin
Saloon Keeper
Posts: 3931
43
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Malcolm wrote:I'm only relating what it says in the (very confusing then) book on P117 in the section on switch expressions.

So the -> can be used in ordinary switch statements then can it?



You're fully right.

Let me summarize so you understand better:

1) Syntax may be an arrow ("new" one ->) or traditional (colon :)
2) A switch can be an expression or a statement (it can be decided by the compiler by looking if the case block returns anything)

These are orthogonal, and totally there could be 4 combinations:
1) switch expression with arrow syntax
2) switch statement with arrow syntax
3) switch expression with colon syntax
4) switch statement with colon syntax

Some authors or articles make the impression that arrow syntax is only for switch expressions, but it's not true (see above 4 combinations)

HTH,
MZ
 
Stephan van Hulst
Bartender
Posts: 15720
367
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Malcolm wrote:I repeat that if what you're both saying is true then the book is awful because this is supposed to prepare people for the exam.


Yes, it's an unfortunate mistake. I believe it was already discussed on this forum before, so it might be corrected in a future edition.

Mikalai Zaikin wrote:A switch can be an expression or a statement (it can be decided by the compiler by looking if the case block returns anything)


A small correction here. The compiler doesn't determine whether the switch is a statement or expression by looking at the switch block. The compiler simply expects a statement or an expression at the current location.

You can very easily do this test for yourself by replacing the entire switch with a simple expression, say "Winter".

In printSeason(), using an expression leads to invalid code, so the switch must be a statement.

In printSeason2(), the code remains valid if we replace the switch with a simple expression. Therefore, the switch is a switch expression.
 
Mikalai Zaikin
Saloon Keeper
Posts: 3931
43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I agree, more accurately to say the compiler decided from the context if this is an expression or statement.

As I said in my first posting -- switch expression is always right-hand side (RHS) member.
 
Stephan van Hulst
Bartender
Posts: 15720
367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure what you mean by "right-hand side member". There is no necessity for the switch expression to be on the right side of an assignment operator. You can put the switch expression anywhere any other expression would be legal. Check out this (admittedly very contrived) example:
 
Dave Malcolm
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for clearing that all up everyone - much appreciated. I have one more question.

In the code I originally posted PrintSeason (which I now know to be a switch statement ) doesn't require a default branch whereas PrintSeason2 (the switch expression) does. Why?

I can even write PrintSeason(13) and the compiler doesn't complain and it runs fine but doesn't produce any output! That seems very wrong!!!
 
Marshal
Posts: 28305
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A switch expression must have a value. (Because it's an expression, and an expression must have a value.) That's why the expression in your example needs a "Default", because otherwise (e.g. if you passed 42 as your parameter) there would be no code to produce the expression's value.

That's because your code switches on an int value. If it switched on a value with finitely many possible values (a boolean, an enum for example) then I suspect that the default would be unnecessary -- check it out and see what you get.

(Yes, I know there are only finitely many int values. Go ahead and write the code which uses all of them and then we can talk.)
 
Stephan van Hulst
Bartender
Posts: 15720
367
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:a boolean


The only primitive types allowed in a selector expression are char, byte, short and int.

I assume they didn't include boolean because instead of a case expression you can more succinctly use a ternary expression.
 
The moth suit and wings road is much more exciting than taxes. Or 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