Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

enum in switch (Thinking in java )

 
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Thinking in java 4th edition, page no. 729
The compiler does not complain that there is no default statement inside the switch, but
that’s not because it notices that you have case statements for each Signal instance. If you
comment out one of the case statements it still won’t complain. This means you will have to
pay attention and ensure that you cover all the cases on your own. On the other hand, if you
are calling return from case statements, the compiler will complain if you don’t have a
default—even if you’ve covered all the possible values of the enum.


but it is wrong, look at my code, I have just commented one case to the book's code :


this code is compiling and running fine . Then why is it written in the book ?
neither the statement for the return is case is true ?
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Puspender Tanwar wrote:


Thinking in java 4th edition, page no. 729
The compiler does not complain that there is no default statement inside the switch, but
that’s not because it notices that you have case statements for each Signal instance. If you
comment out one of the case statements it still won’t complain.


but it is wrong, look at my code, I have just commented one case to the book's code
this code is compiling and running fine . Then why is it written in the book ?



I am missing the point here. The book says that it will still compile if you remove a case. You removed a case, and it still compiled. How is the book wrong?

Henry
 
Marshal
Posts: 8429
606
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Puspender, Henry is right, likely you misread word complain with compile. Complain is the one which mentioned in your quote. "Still won't complain..."
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Liutauras Vilda ,yes you caught it right.. I read it incorrectly .
but, look at the second statement of the book

if you
are calling return from case statements, the compiler will complain if you don’t have a
default—even if you’ve covered all the possible values of the enum.


now look at my code which is running successfully :

hope this time I have read it correctly
 
Liutauras Vilda
Marshal
Posts: 8429
606
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your method is void, and not returning nothing, yes I agree, you're using return, but not actually returning nothing, which is probably what is meant in a book. Give a try by changing your method change to boolean or something and try to return actual literals without having default case, it will give you an error.
Don't know other explanation at the moment.
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote: Give a try by changing your method change to boolean or something and try to return actual literals without having default case, it will give you an error.


NO, it is still compiling fine
 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Puspender Tanwar wrote:
NO, it is still compiling fine



First, from the JLS ...

Java Language Specification wrote:Compilers are encouraged (but not required) to provide a warning if a switch on an enum-valued expression lacks a default case and lacks cases for one or more of the enum type's constants.



Regarding this quote, two points. First point, it mentions "lacks a default case *and* lacks cases for one or more of the enum type's constants". While your example, lacks the default case, it doesn't do the later, so there should be no compile error.

Second point. The JLS only encourages, but does not require, that it be a compiler warning. So, the code, even if it lacked cases for the enum, is not guaranteed to generate compiler warnings.

Henry
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello Henry..
means, book is incorrect(as it said that is will complain ) ??
and can you provide me the link to this JLS topic ?
 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Puspender Tanwar wrote:means, book is incorrect(as it said that is will complain ) ??
and can you provide me the link to this JLS topic ?



Can't help regarding the book -- as I never read it (and hence, don't know if you are taking it out of context). Regarding the JLS, here you go...

https://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.11

Henry
 
Liutauras Vilda
Marshal
Posts: 8429
606
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Puspender Tanwar wrote:NO, it is still compiling fine

Interesting enough, I did try with boolean on my machine and with mycompiler, and I got an error as book tells you.
Henry provided an explanation from JLS and it is interesting, never knew it, as many other things too
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:Interesting enough, I did try with boolean on my machine and with mycompiler, and I got an error as book tells you.
Henry provided an explanation from JLS and it is interesting, never knew it, as many other things too


well, my compiler is a superman, it is still compiling fine for Boolean
 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Puspender Tanwar wrote:
well, my compiler is a superman, it is still compiling fine for Boolean



If you are using an obscure JVM, perhaps you can tell us what it is? And if you are not, perhaps you can show us your latest code? ... as the reason your previous code compiled is because the switch had all the enum cases.

Henry
 
Enthuware Software Support
Posts: 4646
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The statement from the book is incorrect.


On the other hand, if you are calling return from case statements, the compiler will complain if you don’t have a default—even if you’ve covered all the possible values of the enum.


I think the author is talking about a situation when there is no return statement after the switch statement. Obviously, if you don't have a default and if you are also missing an enum in the switch, what will the method return if the switch variable contains the missing value? The compiler complains because of that. Not because of the missing case.
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Anilprem wrote:I think the author is talking about a situation when there is no return statement after the switch statement. Obviously, if you don't have a default and if you are also missing an enum in the switch, what will the method return if the switch variable contains the missing value? The compiler complains because of that. Not because of the missing case.


yes you are right, as compiler complains in such situation. Not only for enum, But for every value that is valid for switch expression(int,char,short,byte, Integer,Character,Byte,Short,String,enum) .
Author have only focused on enum

Henry Wong wrote:If you are using an obscure JVM, perhaps you can tell us what it is? And if you are not, perhaps you can show us your latest code? ... as the reason your previous code compiled is because the switch had all the enum cases.
Henry


sorry Henry, but couldn't understood your message
 
Liutauras Vilda
Marshal
Posts: 8429
606
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Puspender Tanwar wrote:sorry Henry, but couldn't understood your message


I think Henry was right. If your method is meant to return "boolean" and even you do have all cases covered, default case still should ensure that at least default value will be returned, even tho enum is considered as compile time constant, I've seen in some literature that could be changed at run-time (not fully sure about this). Remember, compiler does not execute your code, just compiles. The fact is, that I'm getting compile error even if all cases are covered except default.. So, book is correct indeed.
 
What I don't understand is how they changed the earth's orbit to fit the metric calendar. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic