Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

doubt in switch.......

 
Ranch Hand
Posts: 126
VI Editor Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 126
VI Editor Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 126
VI Editor Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Chandra
 
Guru dhaasan
Ranch Hand
Posts: 126
VI Editor Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Kumar
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guru,
 
Guru dhaasan
Ranch Hand
Posts: 126
VI Editor Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Keith
 
Guru dhaasan
Ranch Hand
Posts: 126
VI Editor Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Chandra
 
Pay attention! Tiny ad!
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic