Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about switch case in core java

 
jinesh parikh
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class jinesh{
public static void main(String args[]){
byte g = 2;
switch(g) {
case 23:
case 128:
}
}
the above piece of code gives me the error as following

Test.java:6: possible loss of precision
found : int
required: byte
case 128:

Now i am confusd here bcz any how the byte is going to be promoted to int
and 128 is with in the range of the int then why compiler is giving me such an error?
 
megha joshi
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

Here the byte argument in switch() is not promoted to int...Remember switch() takes char,byte,short and int( and as of Java 5 enum too) as expression.


What the error msg means is that 128 is a int as the range of byte is from -128 to 127....So all its saying is that it found 128 which is a int...but it was expecting a byte ie. < 128...That is why it fails.
[ April 23, 2007: Message edited by: megha joshi ]
 
souji nutak
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The range of byte is -128 to 127 only, because of this it is giving that error
 
jinesh parikh
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Megha,

then if i had written an expression that uses the byte varriable only in a switch then it wont be promoted to an int?
 
megha joshi
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
give me a example...not able to get the exact meaning of your question?....But yes if you mean to say that the following promotes byte to int...

byte i;

switch(i){

case 1:...
...
}

Then I think it does not...it remains byte...
 
souji nutak
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in switch case the byte will not be promoted as int, it will be byte only. so when you are giving the case as 128 it is giving an error
 
jinesh parikh
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an example megha
please go through it.

this programs works absoluty fine. why?

public class testSwitch {

public static void main(String args[])
{
System.out.println("Testing programme of switch case");
byte b=3;
byte c=3;
switch(b+c){
case 128:
System.out.println("Your value is 128.....");
break;

}
System.out.println("Exiting the programme of switch case");
}
}
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jinesh,



That is because
byte + byte = int
byte + int = int
short + int =int
int + int = int
int + short =int
short + short = int
char + char = int
int + char = int
and so on ...



Regards,
cmbhatt
 
Cao Bin
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In calculation,byte,char autochanged to int.
 
swapnil dangore
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi jinesh,
Remeber two important rules of switch
1)Switch expression should be of any type that can be implicitly cast to an int
2)A case constant shold be of same type as that of the switch expression

Now in your example
a)swaitch(b+c) results to an int
b) and case constant 128 is in range of an int
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic