wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt in switch....... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt in switch......." Watch "doubt in switch......." New topic
Author

doubt in switch.......

Guru dhaasan
Ranch Hand

Joined: Sep 13, 2006
Posts: 126

It is given that switch can take char,byte,short,int and enum.
Consider the following code:

byte g=2;
switch(g){
case 23:
case 128:
}

This wont compile. because byte is implicitly converted to int. 128 is too large for a byte.

My Question is: what is the need for byte and short if that will be implicitly converted to int??

Any explanation appreciated

Regards
Guru


Thanks, Shiv
SCJP, OCE - JSP & Servlets Developer
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Guru,

All the integral literals are int. You know there is not error while you assign byte a int literal but that must be under the byte range, there is no error in assigning int literal to a char but it must be withing range of char and so on. What is implicitely converted to int can be used in the switch. It is mere a flexibility and restraining you to stick only with int would look not ok, because it is provided that there is implicit casting from int to respective (byte,char,short).



Hope you got it better!

Regards,
cmbhatt
[ March 26, 2007: Message edited by: Chandra Bhatt ]

cmbhatt
Srinivas Kumar
Ranch Hand

Joined: Jul 14, 2005
Posts: 52
It's not that byte will always be implicitly converted to int.
The memory occupied for byte is obviously less than that of int.
When you have memory constraints, wherever possible you can use byte instead of int.
For e.g: age of person can be of data type byte/short instead of int.
Here you don't need to have int data type for age.It saves memory.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi VSSM Kumar,

I miss to understand you "byte is not always converted to int". Please clarify.


Note: "A literal integer is always an int"
int a1=100;
byte b1=a1; //compiler error, can't concert int to byte

byte b2=100; //OK, implicit casting takes place

"You should have said: int is not converted to byte implicitely but int literal is casted to byte if the integer literal is withing range of byte"

Thanks and Regards,
cmbhatt
Srinivas Kumar
Ranch Hand

Joined: Jul 14, 2005
Posts: 52
Ok.Let me rephrase it.
"Byte will not always be implicitly converted even when not required."
You are right when byte is assigned to int, it will always be converted to int.But what I mean by the statement is when you declare a byte, it will not implicitly convert itself to int and occupy 32 bits.
Hope it is clear to you what I mean.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi VSSM Kumar,

Obviously, byte would never dare to occupy 32 bits; Here the issue is implicit casting. Again saying what is assigned to a byte or every integral datatype is a "integer value" (one should not get confused with int and integer).

int is a data type where integer is referred as any integral number (that is not having decimal part).

byte occupies 8 bits and it is capable of holding signed number in the range of -2^7 to 2^7-1.

Thanks and regards,
cmbhatt
Guru dhaasan
Ranch Hand

Joined: Sep 13, 2006
Posts: 126

So can I say like in the code following:

byte g=2;
switch(g){
case 23:
case 128:
}

since there is a 128, it is treated as int. so when byte `g` tries to match itself with int it is creating problems

Is it right......... Correct me if I am wrong

Regards
Siva
Guru dhaasan
Ranch Hand

Joined: Sep 13, 2006
Posts: 126

Thanks Chandra
Guru dhaasan
Ranch Hand

Joined: Sep 13, 2006
Posts: 126

Thanks Kumar
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Originally posted by Guru dhaasan:
So can I say like in the code following:

byte g=2;
switch(g){
case 23:
case 128:
}

since there is a 128, it is treated as int. so when byte `g` tries to match itself with int it is creating problems

Is it right......... Correct me if I am wrong

Regards
Siva


No, each case constant has to be assignable to the switch variable.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Guru,
Guru dhaasan
Ranch Hand

Joined: Sep 13, 2006
Posts: 126

Thanks Keith
Guru dhaasan
Ranch Hand

Joined: Sep 13, 2006
Posts: 126

Thanks Chandra
 
jQuery in Action, 2nd edition
 
subject: doubt in switch.......
 
Similar Threads
Marcus Exam #1, Q 26
confused with switch statement
Case expression assignment
switch statement
Switch statement