How do you specify a byte literal in Java?
You can use a byte literal in Java... sort of.
byte f = 0; f = 0xa;
0xa
(int literal) gets automatically cast to byte. It's not a real byte literal (see JLS & comments below), but if it quacks like a duck, I call it a duck.
What you can't do is this:
void foo(byte a) { ...} foo( 0xa ); // will not compile
You have to cast as follows:
foo( (byte) 0xa );
But keep in mind that these will all compile, and they are using "byte literals":
void foo(byte a) { ...} byte f = 0; foo( f = 0xa ); //compiles foo( f = 'a' ); //compiles foo( f = 1 ); //compiles
Of course this compiles too
foo( (byte) 1 ); //compiles