• 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

Selection Statements issue in java

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends,

Please clear my dbt i have with Selection statements in java. Please look at the below lines of code.

Byte b = 5;
final byte i =10;
switch(b){
case i:
break;
case 3: //compilation error
break;
}


For the above code i am getting the compilation error( at case 3 as cannot covert from int to Byte. .Here the value 3 converts to Integer object type and trys to assign that to b.
Is my assumption correct?

If i change the type of b from Byte to byte it compiles fine.

byte b = 5;
final byte i =10;
switch(b){
case i:
break;
case 3:
break;
}


Can you please clarify...

Thanks,
Ravindra.
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By default in Java any constant whole number is type int. You are getting a type mismatch because you are trying to compare an int to a Byte, they are different data types so it gives you an error.

Also, byte is a primitive Byte is an object. Objects don't get compared the same way as primitives. So comparing to int to byte works, because the byte is implicitly cast to an int. However a Byte cannot be implicitly cast to an int.

To compare objects try this Object1.equals(Object 2) will return true if they are equal. Using == on objects doesn't work, the same way as with primitives. The only time you use == on an object is to see if two variables are actually referencing the same Object.

The only time you really use Byte(big B) is when you are converting between types or need to wrap a primitive as an object. Generally stick to primitives if you can.

I hope your head doesn't hurt too much after reading all that.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

Byte b = 5; // this line is the problem
final byte i =10;
switch(b){
case i:
break;
case 3: //compilation error
break;
}

Byte is a class, while byte is a primitive data-type.
you must be creating an instance of Byte using any of the Byte constructors(please refer to API). coming to what you have in mind, you should be using byte and not Byte.

in addition, the expr in switch(<expr> must be of integral type, i.e. the expr can be anyone of byte, short, char or int and only these. so, the case labels must also be assignable types, meaning values of case labels must be within range of the expr.
 
reply
    Bookmark Topic Watch Topic
  • New Topic