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

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

 
Greenhorn
Posts: 5
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:
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.
 
Saloon Keeper
Posts: 15620
366
  • Number of slices to send:
    Optional 'thank-you' note:
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
  • Number of slices to send:
    Optional 'thank-you' note:
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?
 
Bartender
Posts: 3919
43
  • Number of slices to send:
    Optional 'thank-you' note:
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
  • Number of slices to send:
    Optional 'thank-you' note:
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
Bartender
Posts: 3919
43
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:

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
Saloon Keeper
Posts: 15620
366
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:

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
Bartender
Posts: 3919
43
  • Number of slices to send:
    Optional 'thank-you' note:
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
Saloon Keeper
Posts: 15620
366
  • Number of slices to send:
    Optional 'thank-you' note:
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
  • Number of slices to send:
    Optional 'thank-you' note:
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: 28262
95
  • Number of slices to send:
    Optional 'thank-you' note:
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
Saloon Keeper
Posts: 15620
366
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:

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.
reply
    Bookmark Topic Watch Topic
  • New Topic
vceplus-200-125    | boson-200-125    | training-cissp    | actualtests-cissp    | techexams-cissp    | gratisexams-300-075    | pearsonitcertification-210-260    | examsboost-210-260    | examsforall-210-260    | dumps4free-210-260    | reddit-210-260    | cisexams-352-001    | itexamfox-352-001    | passguaranteed-352-001    | passeasily-352-001    | freeccnastudyguide-200-120    | gocertify-200-120    | passcerty-200-120    | certifyguide-70-980    | dumpscollection-70-980    | examcollection-70-534    | cbtnuggets-210-065    | examfiles-400-051    | passitdump-400-051    | pearsonitcertification-70-462    | anderseide-70-347    | thomas-70-533    | research-1V0-605    | topix-102-400    | certdepot-EX200    | pearsonit-640-916    | itproguru-70-533    | reddit-100-105    | channel9-70-346    | anderseide-70-346    | theiia-IIA-CIA-PART3    | certificationHP-hp0-s41    | pearsonitcertification-640-916    | anderMicrosoft-70-534    | cathMicrosoft-70-462    | examcollection-cca-500    | techexams-gcih    | mslearn-70-346    | measureup-70-486    | pass4sure-hp0-s41    | iiba-640-916    | itsecurity-sscp    | cbtnuggets-300-320    | blogged-70-486    | pass4sure-IIA-CIA-PART1    | cbtnuggets-100-101    | developerhandbook-70-486    | lpicisco-101    | mylearn-1V0-605    | tomsitpro-cism    | gnosis-101    | channel9Mic-70-534    | ipass-IIA-CIA-PART1    | forcerts-70-417    | tests-sy0-401    | ipasstheciaexam-IIA-CIA-PART3    | mostcisco-300-135    | buildazure-70-533    | cloudera-cca-500    | pdf4cert-2v0-621    | f5cisco-101    | gocertify-1z0-062    | quora-640-916    | micrcosoft-70-480    | brain2pass-70-417    | examcompass-sy0-401    | global-EX200    | iassc-ICGB    | vceplus-300-115    | quizlet-810-403    | cbtnuggets-70-697    | educationOracle-1Z0-434    | channel9-70-534    | officialcerts-400-051    | examsboost-IIA-CIA-PART1    | networktut-300-135    | teststarter-300-206    | pluralsight-70-486    | coding-70-486    | freeccna-100-101    | digitaltut-300-101    | iiba-CBAP    | virtuallymikebrown-640-916    | isaca-cism    | whizlabs-pmp    | techexams-70-980    | ciscopress-300-115    | techtarget-cism    | pearsonitcertification-300-070    | testking-2v0-621    | isacaNew-cism    | simplilearn-pmi-rmp    | simplilearn-pmp    | educationOracle-1z0-809    | education-1z0-809    | teachertube-1Z0-434    | villanovau-CBAP    | quora-300-206    | certifyguide-300-208    | cbtnuggets-100-105    | flydumps-70-417    | gratisexams-1V0-605    | ituonline-1z0-062    | techexams-cas-002    | simplilearn-70-534    | pluralsight-70-697    | theiia-IIA-CIA-PART1    | itexamtips-400-051    | pearsonitcertification-EX200    | pluralsight-70-480    | learn-hp0-s42    | giac-gpen    | mindhub-102-400    | coursesmsu-CBAP    | examsforall-2v0-621    | developerhandbook-70-487    | root-EX200    | coderanch-1z0-809    | getfreedumps-1z0-062    | comptia-cas-002    | quora-1z0-809    | boson-300-135    | killtest-2v0-621    | learncia-IIA-CIA-PART3    | computer-gcih    | universitycloudera-cca-500    | itexamrun-70-410    | certificationHPv2-hp0-s41    | certskills-100-105    | skipitnow-70-417    | gocertify-sy0-401    | prep4sure-70-417    | simplilearn-cisa    |
http://www.pmsas.pr.gov.br/wp-content/    | http://www.pmsas.pr.gov.br/wp-content/    |