Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Does the switch statement accepts a byte to evaluate?

Status
Not open for further replies.

LuckyStarr

Programmer
Sep 12, 2002
22
0
0
BR
Hi all,

I'm getting a very strange exception in my code and I started suspecting about a switch statement.

I noticed that it evaluates a byte, but I've never seen a code like this, I learned that the switch only evaluates integer expressions. Even the java tutorial doesn't mention anything about bytes, only integers.

Is the code wright or wrong? I would test myself, but the problem only occurs with a very far away customer.

Example Code:

public static final byte FORM_DATE = 3;

...

byte form = ...;

...

try{
switch(form){

case FORM_DATE: "do something"
break;

default: throw new IllegalArgumentException( "Invalid form = " + form );
}
}


---
There is no Knowledge that is not power
---
 
Code:
class sw
      {
       public static void main(String args[])
              {
               int a = 1;
               byte b = -128; // The range of b should be between -128 and +127
               char c = 1;
               short d = 1;
               long l = 1;
             
               switch(a)
                     {
                      case 1:System.out.println("a");
                     }
               
               switch(b)
                     {
                      case 1:System.out.println("b");
                     }
               switch(c)
                     {
                      case 1:System.out.println("c");
                     }
               switch(d)
                     {
                      case 1:System.out.println("d");
                     }
               switch((int)l)
                     {
                      case 1:System.out.println("l");
                     }

              }
      }
 
I will take this as a "Yes, the switch can evaluate a byte".

Thanks



---
There is no Knowledge that is not power
---
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top