File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Byte Literal Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Byte Literal" Watch "Byte Literal" New topic
Author

Byte Literal

Tony Tirolese
Greenhorn

Joined: Nov 20, 2011
Posts: 12

What is the literal binary value for a byte type variable holding a decimal value of -128?

I have been able to do this:


But I have found that I cannot do this:


What am I missing/not understanding?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

0b10000000 is an int literal that is equal to 128. That means that it is outside the range of byte, and you need to explicitly cast it to byte. There are no byte literals, only int literals that are within the range of byte.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Tony Tirolese
Greenhorn

Joined: Nov 20, 2011
Posts: 12

Rob Spoor wrote:0b10000000 is an int literal that is equal to 128. That means that it is outside the range of byte, and you need to explicitly cast it to byte. There are no byte literals, only int literals that are within the range of byte.




(I'm not arguing, just trying to clear my own confusion.)

It seems to me from my reading of available reference material that -128 should be represented in binary for an 8-bit type in Java by the value 1000000 (a one followed by seven zeros).

When I assign the value 01111111 (a zero followed by seven ones) to a byte variable, I don't get any errors and everything runs fine, with no explicit cast required:


I assumed that the unary NOT action in the use of a two's complement value to initialize the b128 required an explicit cast because of possible type promotion to integer on account of using the unary NOT operator.

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

Tony Tirolese wrote:
I assumed that the unary NOT action in the use of a two's complement value to initialize the b128 required an explicit cast because of possible type promotion to integer on account of using the unary NOT operator.



You are way over thinking it here. Literals are compile time constants. An explicit cast of a compile time constant is a compile time constant. Just cast it to a byte and you are done. It doesn't make the code (class file) longer. There isn't any int value that gets converted to a byte. etc. A cast of a literal just tells the compiler to treat it as a byte value.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Tony Tirolese
Greenhorn

Joined: Nov 20, 2011
Posts: 12

Is the high order bit used only for determining whether a value is positive or negative?

Tony Tirolese
Greenhorn

Joined: Nov 20, 2011
Posts: 12

Henry Wong wrote:
Tony Tirolese wrote:
I assumed that the unary NOT action in the use of a two's complement value to initialize the b128 required an explicit cast because of possible type promotion to integer on account of using the unary NOT operator.



You are way over thinking it here. Literals are compile time constants. An explicit cast of a compile time constant is a compile time constant. Just cast it to a byte and you are done. It doesn't make the code (class file) longer. There isn't any int value that gets converted to a byte. etc. A cast of a literal just tells the compiler to treat it as a byte value.

Henry


Thanks for your answer. I'm just trying to get a grip on how this works. What I don't understand is why I can assign the value of 127, expressed as a literal in binary format, but I can't do that for -128, when the range of values for a byte is listed as -128 to 127 inclusive.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

Tony Tirolese wrote:Is the high order bit used only for determining whether a value is positive or negative?


I don't know. I don't have Java 7 installed to try it. Why don't you try it and tell us?




Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

Tony Tirolese wrote:Is the high order bit used only for determining whether a value is positive or negative?


Just downloaded Java 7 -- and tested it.

Yes. It looks like with binary literals, Java does use the high bit for negative -- meaning it doesn't report an out of range literal. And it is smart enough to apply the compile time constant conversion rules to it.

The Code:



The Result:

-128


Henry
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

The same rules that apply for decimal literals apply to binary literals. Literal values between -128 and 127 can be assigned to bytes without casting, no matter if they are represented in decimal (-128), binary(0b1111_1111_1111_1111_1111_1111_1000_0000), hexadecimal (0xFFFFFF80) or octal (037777777600).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Byte Literal
 
Similar Threads
Java Primitive Literals
confusion in casting..
What's up with these literal assignments?
complement in byte if range exceeds
Method Overloading with default param values