• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

byte literals

 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to Sun the answer is no.

Garrett
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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 ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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").
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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.
 
And inside of my fortune cookie was this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic