This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt in switch.......

 
Guru dhaasan
Ranch Hand
Posts: 126
Java Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Srinivas Kumar
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Java Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • 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
Java Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Chandra
 
Guru dhaasan
Ranch Hand
Posts: 126
Java Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Kumar
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guru,
 
Guru dhaasan
Ranch Hand
Posts: 126
Java Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Keith
 
Guru dhaasan
Ranch Hand
Posts: 126
Java Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Chandra
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic