• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

about switch case in core java  RSS feed

 
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?
 
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 ]
 
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");
}
}
 
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
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In calculation,byte,char autochanged to int.
 
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
Boost this thread!