283

If I have a method

void f(byte b);

how can I call it with a numeric argument without casting?

f(0);

gives an error.

1
  • 1
    @oliholz that's downcasting with additional parsing overhead
    – Ben Barkay
    Commented May 5, 2013 at 13:44

6 Answers 6

333

You cannot. A basic numeric constant is considered an integer (or long if followed by a "L"), so you must explicitly downcast it to a byte to pass it as a parameter. As far as I know there is no shortcut.

1
  • 20
    If you're doing a lot of this sort of thing, you can define a simple helper method byte b(int i) { return (byte) i; } somewhere and statically import it. Then you can write f(b(10)). Commented Oct 11, 2013 at 18:56
149

You have to cast, I'm afraid:

f((byte)0);

I believe that will perform the appropriate conversion at compile-time instead of execution time, so it's not actually going to cause performance penalties. It's just inconvenient :(

1
  • 16
    +1 for compile-time conversion. It's common sense if you both understand compilers and have faith in language designers (which we should), but otherwise not so obvious. Commented Nov 13, 2013 at 5:29
37

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
5
  • 23
    These are not byte literals. They are literals of a variety of other types (int, mostly) that are being implicitly converted to a byte. e.g., 1 is an int literal, but double d = 1; compiles just fine.
    – smehmood
    Commented Aug 5, 2014 at 2:18
  • If you're already using tricks. Have a static import of byte b(int i){}, then b(1) as long and less tricky than f=1. Commented Aug 20, 2014 at 9:04
  • 1
    @smehmood, But since the conversion is done by the pre-compiler/compiler (before the program even starts running) and not the runtime, then it is a literal isn't it?
    – Pacerier
    Commented Sep 8, 2014 at 3:08
  • 3
    @Pacerier It is a literal. It is not a "byte literal". It is an int. The compiler treats it as an int literal (as it should) and does an implicit downcast in the assignment (as it also should). At no point is it parsed as a "byte literal" (which does not exist). See JLS Section 5.2 in particular the latter half concerning narrowing conversions. The only things involved are an integer constant and the application of an appropriate assignment conversion rule to a byte variable.
    – Jason C
    Commented Feb 26, 2015 at 3:12
  • I gave this answer +1 because the technique is novel, but indeed, there ain't no "byte literals" in Java.
    – user719662
    Commented Jan 14, 2016 at 21:45
13

If you're passing literals in code, what's stopping you from simply declaring it ahead of time?

byte b = 0; //Set to desired value.
f(b);
4
  • 3
    This also allows you to give the value a more semantic name. en.wikipedia.org/wiki/… Commented May 9, 2012 at 13:31
  • This is useful. If you're trying to fill an array of bytes using java's 'fill' method, this is most sensible.
    – user1086498
    Commented Jul 11, 2012 at 20:01
  • The compiler just complained about the following, however, and I needed to add the cast: public static final byte BYTE_MASK = ( byte )0xff;
    – Marvo
    Commented Jul 27, 2012 at 20:45
  • And I realized that I actually wanted byte BYTE_MASK = 0x000000ff; lest I get some nasty sign extension bugs.
    – Marvo
    Commented Jul 27, 2012 at 21:18
5

What about overriding the method with

void f(int value)
{
  f((byte)value);
}

this will allow for f(0)

4
  • 32
    -1 This is very bad for code readability. And could cause problems when people actually try to pass in a value higher than the byte can hold. I discourage people from using this method!
    – Rolf ツ
    Commented Jun 28, 2014 at 18:56
  • 4
    Also, this cast will happen at run-time. Very bad. Commented Apr 18, 2015 at 4:34
  • Completely agreeing with Rolf (Tsu), it's perhaps worth adding, that technically it's overloading, not overriding.
    – Cromax
    Commented Oct 10, 2017 at 13:43
  • This is not how you should use overriding, and this can inject a lot of errors for the users. casting is something that ensures the type safety.
    – Agnibha
    Commented Feb 26, 2022 at 6:20
-3

With Java 7 and later version, you can specify a byte literal in this way: byte aByte = (byte)0b00100001;

Reference: http://docs.oracle.com/javase/8/docs/technotes/guides/language/binary-literals.html

2
  • 27
    binary literal != byte literal. Commented Sep 17, 2015 at 8:57
  • 1
    you are still down casting to byte. Commented Dec 17, 2018 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.