Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Implicit cast For Switch Statement expression required

 
Ranch Hand
Posts: 77
2
Netbeans IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi Mr. Deskmukh


In Switch statement, the expression implicitly required to cast. This also applies to the constant expressions in the case statements that follows the switch:

Error occurred in the line "case 127+1"

Note: The other types are float, long and double are not valid as described in Java Specification.


My question is about, "Does it mean the switch expression and case expression must be similiar type? "

Thank you



 
Bartender
Posts: 1244
86
Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Noorul Hameed wrote:Error occurred in the line "case 127+1"

Didn't It give compile time error at line 1? Please check once again.

My question is about, "Does it mean the switch expression and case expression must be similiar type? "

Case constant must be assignment compitable with expression...  JLS 10 The switch Statement

JLS 10, 14.11. The switch Statement wrote:Every case constant associated with the switch statement must be assignment compatible with the type of the switch statement's Expression


Since in your example Switch statement's expression is of type byte whose range is -128 to 127 and in switch statement's case you have 127 + 1  results 128 which doesn't fit in the range of byte (-128 To 127 ) so gives error.
If it was 126 + 1 results in 127 which is by default int literal still would run successfully because 127 is assignment compatible with byte means It fits in the range of byte data type.  

Edit: 127 is implicitly narrowed to byte...
 
Marshal
Posts: 65023
246
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ganesh Patekar wrote:. . . 127 is implicitly narrowed to byte...

But that sort of thing can only occur without an explicit cast when you are declaring and initialising a variable on the same line, and the expression right of the = is a compile‑time constant.
 
Ganesh Patekar
Bartender
Posts: 1244
86
Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Ganesh Patekar wrote:. . . 127 is implicitly narrowed to byte...

But that sort of thing can only occur without an explicit cast when you are declaring and initialising a variable on the same line, and the expression right of the = is a compile‑time constant.

I don't think so It can happen in Switch statement's case constant and expression assignment as well. I tried to compile the following codeIt gave compile time error:

C:\my>javac Test.java
Test.java:6: error: incompatible types: possible lossy conversion from int to byte
               byte no = 127 + 1;
                             ^
Test.java:11: error: incompatible types: possible lossy conversion from int to byte
               case 128:
                    ^

In both cases It tried implicitly narrowing compile time constant value 128 ( int literal ) to byte so gave same error message for both i.e. possible lossy conversion from int to byte means at both places It tried to convert those values into byte but couldn't.
 
Campbell Ritchie
Marshal
Posts: 65023
246
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ganesh Patekar wrote:. . . I don't think so It can happen in Switch statement's case constant and expression assignment as well. . . .

Yes, I think you are right; the compiler is trying to narrow those compile‑time constants to bytes. This is what the Java® Language Specification (=JLS) says:-

Every case constant associated with the switch statement must be assignment compatible with the type of the switch statement's Expression (§5.2).

. . . and this is from §5.2:-

. . . if the expression is a constant expression (§15.28) of type byte, short, char, or int:

A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable.

 
Ganesh Patekar
Bartender
Posts: 1244
86
Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
 
Campbell Ritchie
Marshal
Posts: 65023
246
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and §5.2 says it “may” perform a narrowing conversion, not “does perform”.
 
Noorul Hameed
Ranch Hand
Posts: 77
2
Netbeans IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Noorul Hameed wrote:

Yeah, typo error, it can be byte temp=22 only

 
Campbell Ritchie
Marshal
Posts: 65023
246
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe it wasn't a good idea to use bytes, which are really only suitable for sending data across networks in arrays.
 
Noorul Hameed
Ranch Hand
Posts: 77
2
Netbeans IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Maybe it wasn'/quote]

Yes. Its true. It is also used for signed integer number notations like short, int, and long.

Encryption and decryption also applied it.

 
Ganesh Patekar
Bartender
Posts: 1244
86
Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:. . . and §5.2 says it “may” perform a narrowing conversion, not “does perform”.

Oh I see got now, yes I said "tried" where It actually didn't but just warned us.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!