This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes about switch case in core java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "about switch case in core java " Watch "about switch case in core java " New topic
Author

about switch case in core java

jinesh parikh
Ranch Hand

Joined: Apr 20, 2007
Posts: 48
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

Joined: Feb 20, 2007
Posts: 206
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

Joined: Mar 15, 2007
Posts: 8
The range of byte is -128 to 127 only, because of this it is giving that error
jinesh parikh
Ranch Hand

Joined: Apr 20, 2007
Posts: 48
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

Joined: Feb 20, 2007
Posts: 206
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

Joined: Mar 15, 2007
Posts: 8
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

Joined: Apr 20, 2007
Posts: 48
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

Joined: Feb 28, 2007
Posts: 1707
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


cmbhatt
Cao Bin
Greenhorn

Joined: Apr 23, 2007
Posts: 14
In calculation,byte,char autochanged to int.


i love you not because who you are but because who i am when i'm with you
swapnil dangore
Ranch Hand

Joined: Jun 05, 2006
Posts: 46
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


Cheers<br />-------------<br />Swapnil<br /> <br />SCJP5-81%<br /> <br />"Dictionary is the only place where Success come before Work"
 
jQuery in Action, 2nd edition
 
subject: about switch case in core java