如何在Java中指定字节文字? [英] How do you specify a byte literal in Java?

查看:131
本文介绍了如何在Java中指定字节文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有方法

void f(byte b);

如何在不进行投射的情况下使用数字参数调用它?

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

f(0);

给出错误。

推荐答案

你做不到。基本数字常量被视为整数(如果后跟L,则为long),因此必须将其明确地向下转换为一个字节以将其作为参数传递。据我所知,没有捷径。

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.

这篇关于如何在Java中指定字节文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆