What i know is:
In java, all byte and short data types are automatically promoted to int during a calculation. And, if you are casting a large data type like int to a byte then the int is reduced modulo the range of the short data type i.e byte (the range of byte is 256).
The program below has no compilation error. The output is incorrect for the first print statement..plea se explain why?
[CODE=Java]class TestByte
{
public static void main(String args[])
{
byte b = 50, c=50;
int i;
i=b*60;
b=(byte)(b*60);
c=(byte)(c*6);
System.out.prin tln(b); // should be 184
System.out.prin tln(c); // should be 44
System.out.prin tln(3000%256); //should be 184
System.out.prin tln(i); // should be 3000
}
}[/CODE]
Output:
-72
44
184
3000
In java, all byte and short data types are automatically promoted to int during a calculation. And, if you are casting a large data type like int to a byte then the int is reduced modulo the range of the short data type i.e byte (the range of byte is 256).
The program below has no compilation error. The output is incorrect for the first print statement..plea se explain why?
[CODE=Java]class TestByte
{
public static void main(String args[])
{
byte b = 50, c=50;
int i;
i=b*60;
b=(byte)(b*60);
c=(byte)(c*6);
System.out.prin tln(b); // should be 184
System.out.prin tln(c); // should be 44
System.out.prin tln(3000%256); //should be 184
System.out.prin tln(i); // should be 3000
}
}[/CODE]
Output:
-72
44
184
3000
Comment