File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Byte.MIN_VALUE 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 "Byte.MIN_VALUE" Watch "Byte.MIN_VALUE" New topic


Ramya Iyer
Ranch Hand

Joined: Jul 29, 2003
Posts: 66
Can anyone please explan why I am getting this output. I thouht Byte.MIN_VALUE should be - 2 to the power of 7 and hence its binary representation would be

2(power) 7 is represented as 10000000
complement 01111111
-2(power) 7 is 10000000

But this is not the represention in the output in line 3, and line 4 for hex

C:\>java test
Byte.MAX_VALUE in Binary:1111111
Byte.MAX_VALUE in Hex:7f
Byte.MIN_VALUE in Binary:11111111111111111111111110000000 //o/p line 3
Byte.MIN_VALUE in Hex:ffffff80 // o/p line 4

I have used Integer.toHexString function passing Byte.MAX_VALUE and Byte.MIN_VALUE, also used Integer.toBinaryString.

marc weber

Joined: Aug 31, 2004
Posts: 11343

Integer methods toBinaryString and toHexString both take int as an argument. So when you pass a byte, it's automatically converted to a 32-bit int.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Henry Wong

Joined: Sep 28, 2004
Posts: 20516

The toHexString() and toBinaryString() methods takes an int -- not a byte. The MIN_VALUE value has to be casted to an int first prior to calling the method. What you are seeing is the bit / hex pattern of the value after the cast has occurred.

BTW, this is true with MAX_VALUE too, but you don't see it because the cast conversion is simply a padding of lots of zeros.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ramya Iyer
Ranch Hand

Joined: Jul 29, 2003
Posts: 66
Thanks Marc and Henry,

I understand that the value is getting converted into an int. In the first case for MAX_VALUE, we have zero's (sign bit) padded to the left and in the second case MIN_VALUE, we have sign bit padded to the left, which in this case is 1.
I agree. Here's the link:
subject: Byte.MIN_VALUE
It's not a secret anymore!