aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question on byte Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question on byte" Watch "question on byte" New topic
Author

question on byte

Abhishek Reddy
Ranch Hand

Joined: Mar 28, 2006
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..


Abhishek
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
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 ]

Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: question on byte