0
点赞
收藏
分享

微信扫一扫

How do you specify a byte literal in Java?

一只1994 2023-04-11 阅读 100


问题: If I have a method

void f(byte b);
void f(byte b);

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

f(0); gives an error.

答案:
f(0);

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.

f((byte)0);
f((byte)0);



举报

相关推荐

0 条评论