Win a copy of Modern frontends with htmx this week in the Spring 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
  • 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

char and int behaviour with switch block.

 
Ranch Hand
Posts: 31
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All ,
I have a question regarding char and int when use with switch block.

Following code is concept i know about char and int :


But when same concept i try to apply in switch block as below it fails why? what am i missing here?

Please help me to understand this concept.
Also if there are rules which are critical about switch construct please share them.
 
Ranch Hand
Posts: 472
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

this is like (i mean that in swich you can use literal as it fit into condition in switch)

internally char is unsigned integer and you can use literal as long as it fit in it.
also all letters of english alphabet (big and smal) fit into byte
 
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

aditya chaudhari wrote:


You only get possible loss of precision errors when you try to assign the value of one variable to another variable.
Here there is no assignment happening, only comparison.
The switch statement is equivalent to

You don't assign anything to c, you just compare its value.
 
Sergej Smoljanov
Ranch Hand
Posts: 472
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I mean from JLS 7: 14.11. 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.


that`s why i mention on assignment.
and because you use constant expression in case i use final modifier in assignment example.
 
aditya chaudhari
Ranch Hand
Posts: 31
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sergej and Joanne Neal Thank you for valuable reply.

The two things you both have menioned about my doubt.I am summarizing your explanation.


Sergej :
char is unsigned integer and you can use literal as long as it fit in it

Joanne :
You don't assign anything to switch expression , you just compare its value with case labels.


My Conclusion about switch expression and labels.

we actually compare expression with case values in switch block.the value to compare must be literal values.

I have written following program to play around it might help others..

Also please correct me if any wrong i have understood.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

aditya chaudhari wrote:Sergej :
char is unsigned integer and you can use literal as long as it fit in it


True! And remember all other primitive data types are signed. So when you try to assign a byte to a char, you'll need an explicit cast. Otherwise your code won't compile. Illustrated in this code snippet:


aditya chaudhari wrote:Joanne :
You don't assign anything to switch expression , you just compare its value with case labels.


Again true! With an if-condition there are no data type restrictions (you can compare an int with a byte or a char), BUT with a switch statement the data type of its expression determines which case labels are allowed. The case labels must be constant expressions: either using a literal or a final variable. Illustrated in another code snippet:


Hope it helps!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic