• 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
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Question related to type of switch expression and case constant

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have
it runs fine. Note that the switch expression is of type short (2 bytes) and the case constant is of type int (4 bytes).

My question is: Is the type irrelevant as long as the value is within the boundaries of the type of the switch expression?

I have the feeling that this is true since
this runs fine again, but if I change the literal assigned to i to 128, which is out of range for type byte, then the compiler complains.

Is it true that in the first example the short variable and in the second example the byte variable (the switch expressions) are first implicitly converted to an int and then compared with the case constants?
 
Marshal
Posts: 74009
332
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will have to go through the Java Language Specification about the switch statement, and widening conversions in order to answer that question. I would suggest you look at this FAQ, too.
Also tell us the full details; where was the compiler error? Was it about exceeding the range of a byte or about the switch statement?
 
author
Posts: 23907
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

Campbell Ritchie wrote:You will have to go through the Java Language Specification about the switch statement, and widening conversions in order to answer that question.



As an FYI only, the exact part of the JLS that discusses the compile time constants is a little further down -- in the next subsection...

http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.2

Although, it may be a good idea to read the section pointed to by Campbell first -- and read it all the way to this part. And probably, the next two sections too (all the sections on conversions).

Henry
 
Bora Sabrioglu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You will have to go through the Java Language Specification about the switch statement, and widening conversions in order to answer that question. I would suggest you look at this FAQ, too.


Thanks for the answer... In future I'll try not to get hung up on every little question mark that pops up in my head!

Also tell us the full details; where was the compiler error? Was it about exceeding the range of a byte or about the switch statement?

This is what I get when in the example I replace the literal at line 02. with the literal 128. It was about the exceeding range of a byte.
(Of course, since 128 is out of range for a byte)
But after having read the FAQ 'Don't sweat it' I don't think it's *that* important to know *every* little detail about Java there is... my primary goal for now is - as it says in the FAQ - to become a good Java programmer...
 
Bora Sabrioglu
Ranch Hand
Posts: 100
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I found the solution in the Java Language Specification about the switch statement:

All of the following must be true, or a compile-time error occurs:

Every case constant expression associated with a switch statement must be assignable (ยง5.2) to the type of the switch Expression.

No two of the case constant expressions associated with a switch statement may have the same value.

No switch label is null.

At most one default label may be associated with the same switch statement.



It says here that the "case constant expression" must be assignable to the type of the switch statement... I conclude that only the literal counts that comes out of the "case constant expression"... and that literal has to be within the bounds of the type of the switch expression... thus the type of the "case constant expression" doesn't really matter... only its value.
 
Campbell Ritchie
Marshal
Posts: 74009
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot assign 128 to a byte.
 
Campbell Ritchie
Marshal
Posts: 74009
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And well done finding those details in the JLS. It is by no means easy to navigate.
 
We can walk to school together. And we can both read this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic