Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

char and int behaviour with switch block.

 
aditya chaudhari
Ranch Hand
Posts: 31
Java
  • Mark post as helpful
  • send pies
  • 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.
 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Mark post as helpful
  • send pies
  • 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
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 467
10
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic