• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

switch comparison vs == comparison

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(1)
byte b = 10;
switch(b)
{
case 128:
System.out.println("here");
}
results in a compile time error.

But

(2)
byte b = 10;
integer i = 128;
System.out.println(b == i);
runs fine and produces a result of false.

Question:

Why byte can't be compared to int in (1), whereas byte can be compared to int in (2) ?
Alternatively, why code in (1) cannot compare byte and int values (but will result in loss of precision), whereas code in (2) could compare a byte and int fine ?
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the JLS

Every case constant expression associated with a switch statement must be assignable to the type of the switch Expression.


There is no such restriction for if conditions (or I must say the == operator). When you compare byte == int, the byte is promoted to int, but this doesn't happen in switch statements...
 
Larry Olson
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see. So it is the Java language specification that tells this. But it seems unnatural to force the switch statement to this behavior whereas it isn't the case while using == operator. One of those weird, strange quirks one has to remember
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you think about it, it is logical. The == operator allows comparison between byte and int because you might compare two variables i.e. something like this
So the compiler cannot tell whether i's value will be in the range of byte or not. But in the case of a switch statement, you cannot use variables as case values, so at the compile time you already know whether the case value is assignable to the switch expression or not. What is the whole point of allowing a case which can never be true?? In the example you showed, the case 128 can never be true, so what is the point of allowing it?? By flagging this as an error, the compiler helps you avoid such circumstances as its definitely a logical error...
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A case constant must evaluate to the same type as the switch expression can use, in your case, can 128 to be evaluated to byte? Definitely No!
 
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you use object for switch comparison what kind of evaluation will be performed? Stay tuned :P
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In objects, enum only can be checked with switch - case.
 
The harder you work, the luckier you get. This tiny ad brings luck - just not good luck or bad luck.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic