File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes byte literals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "byte literals" Watch "byte literals" New topic
Author

byte literals

Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Okay, perhaps this belongs in JiG(b). I don't consider myself a beginner, though, so I have a hard time making myself post questions there.

Anyways, is there a way to specify a byte literal, other than an explicit cast? I know you can specify a long literal by appending an L at the end (e.g. 10L). Is there something similar for byte?

Layne


Java API Documentation
The Java Tutorial
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
According to Sun the answer is no.

Garrett


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Originally posted by Layne Lund:

Anyways, is there a way to specify a byte literal, other than an explicit cast?

Layne


No, not even with an explicit cast. This can also be said for the primitive type short. Literals are of type int, long, float, double, boolean, char or String and never anything else.


Tony Morris
Java Q&A (FAQ, Trivia)
Joni Salonen
Ranch Hand

Joined: Jan 07, 2006
Posts: 53
If the literal is in the range 0-255 (instead of -128-127 when the conversion from int literal to byte is automatic) you can subtract 256 to get the value you want. This is identical to casting to byte.

(I'm referring to assignment conversion)
[ January 22, 2006: Message edited by: Joni Salonen ]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Tony Morris:


No, not even with an explicit cast. This can also be said for the primitive type short. Literals are of type int, long, float, double, boolean, char or String and never anything else.


I'm not sure about the exact details, but I had to use a cast in my situation. I am trying to pass a literal as a method argument where a byte is expected. An explicit cast got rid of the compiler error. My guess is that it generates byte codes to do this rather than changing the actual type of the literal.

Layne
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Joni Salonen:
If the literal is in the range 0-255 (instead of -128-127 when the conversion from int literal to byte is automatic) you can subtract 256 to get the value you want. This is identical to casting to byte.

(I'm referring to assignment conversion)

[ January 22, 2006: Message edited by: Joni Salonen ]


Unfortunately, I am passing a parameter, not initializing or assigning to a variable. In this case, it seems I have to cast the literal to a byte to get it to compile.

Thanks for your help, everyone.

Layne
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Layne]: Anyways, is there a way to specify a byte literal, other than an explicit cast?

[Tony]: No, not even with an explicit cast. This can also be said for the primitive type short. Literals are of type int, long, float, double, boolean, char or String and never anything else.

[Layne]: I'm not sure about the exact details, but I had to use a cast in my situation.


Tony's point is that the literal itself is still an int. The expression, including the cast, evaluates as a byte, but the literal is an int.

One way to circumvent the explicit cast is to use the literal to initialize a byte variable:

That's not terribly exciting, but it works. It doesn't change the fact that the literal still has type int as far as the compiler is concerned. It's just that the compiler looks at that int and converts it to a byte in the classfile.

In general though, I'd just use casts whenever necessary here. It's mildly odd that they don't have a way to specify byte literals or short literals, but not a big deal really. Are you having some problem because of the casts, or were you just wondering if there was another way?
[ January 22, 2006: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Thanks Jim. I would have guessed that the compiler generates byte codes to do the conversion from an int literal to a byte value that can be passed as a parameter. I just wasn't sure if that's what Tony meant. It makes sense that the literal still has type int.

Normally I would create a static variable, but it doesn't seem to bring any benefit in this particular situation since I'm only using it once.

Layne
[ January 22, 2006: Message edited by: Layne Lund ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Layne Lund:

Normally I would create a static variable, but it doesn't seem to bring any benefit in this particular situation since I'm only using it once.


One possible benefit I could see is that you are giving it a meaningful name (instead of using a "magic number").


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Layne]: I would have guessed that the compiler generates byte codes to do the conversion from an int literal to a byte value that can be passed as a parameter.

Well if you were dealing with an int variable which is not a constant, then that's what the compiler would do, sure. But since the compiler knows upfront that the number is a constant, and it's used in a context where it must be converted to a byte, the compiler just does that in bytecode, inlining it directly into the code. At least, Sun's current compiler seems to inline byte, short, and any char, int, or long which is small enough to fit into two bytes. The rest (plus floats and doubles) are put in the constant table instead. You can see this using javap if you're curious. It's possible other compilers do something different.

Note that the idea of a "literal", as defined in the JLS, is really only something that exists in the source file. However it gets rendered in the class file, it's no longer a literal in the sense we were using the term.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: byte literals