Win a copy of Penetration Testing Basics this week in the Security forum!

# question on byte

Abhishek Reddy
Ranch Hand
Posts: 259
class TestFundas
{
public static void main(String[] args)
{
byte b4 =0x80;// line 1
System.out.println("Hello World!"+b4);
}
}

why line 1 results in compile time error....eventhough 0x80 falls in the range of byte..
The bit representation of b4 is 1000 0000 which is equal to -128 and this value falls in the range of byte (-128 to 127)..
can any one give me the reason for compile time error..

Naseem Khan
Ranch Hand
Posts: 809
The range which you wrote (-128 to +127) is in decimal representation not in hexadecimal. So before finding any number that whether its is in range of byte or not, first you need to connvert it into decimal.

80 is in hexadecimal and its decimal equivalent is 128 which is certainly outside the range of byte.

Naseem
[ August 29, 2006: Message edited by: Naseem Khan ]

Keith Lynn
Ranch Hand
Posts: 2409
Originally posted by Abhishek Reddy Chepyala:
class TestFundas
{
public static void main(String[] args)
{
byte b4 =0x80;// line 1
System.out.println("Hello World!"+b4);
}
}

why line 1 results in compile time error....eventhough 0x80 falls in the range of byte..
The bit representation of b4 is 1000 0000 which is equal to -128 and this value falls in the range of byte (-128 to 127)..
can any one give me the reason for compile time error..

The problem is that 0x80 is an int constant, and so, even though the bit representation is 10000000, the 1 is not the sign bit since an int is 32 bits.

0x80 = 128 in decimal which is outside the range of byte.
[ August 29, 2006: Message edited by: Keith Lynn ]