jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes Selection Statements issue in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Selection Statements issue in java" Watch "Selection Statements issue in java" New topic
Author

Selection Statements issue in java

Ravindranath Chowdary
Ranch Hand

Joined: Nov 08, 2006
Posts: 71
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.
Jon Parise
Ranch Hand

Joined: Jul 03, 2007
Posts: 81
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.
Michael Jennings
Greenhorn

Joined: Jul 04, 2007
Posts: 20
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Selection Statements issue in java
 
Similar Threads
Switch statement - Constant expression
Compile time constant
Switch statement/case restrictions
Problem With Very Simple While Loop
switch() construct