设置短值Java [英] Setting Short Value Java

查看:133
本文介绍了设置短值Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在J2ME中编写了一些代码。我有一个方法 setTableId(Short tableId)。现在,当我尝试编写 setTableId(100)时,它会产生编译时错误。如何在不声明另一个短变量的情况下设置短值?

I am writing a little code in J2ME. I have a class with a method setTableId(Short tableId). Now when I try to write setTableId(100) it gives compile time error. How can I set the short value without declaring another short variable?

当设置 Long 值时,我可以使用 setLongValue(100L),它的工作原理。那么, L 在这里意味着什么?值的字符是什么?

When setting Long value I can use setLongValue(100L) and it works. So, what does L mean here and what's the character for Short value?

谢谢

推荐答案

在Java中,整数文字的类型为int,除非它们后缀有字母L或l(首都L是首选,因为小写字母L很难与数字1区分)。如果以L为后缀,则文字的类型为long。

In Java integer literals are of type int unless they are suffixed with letter 'L' or 'l' (Capital L is preferred as lower case L is hard to distinguish from number 1). If suffixed with L, the literals are of type long.

后缀在Java语言规范中没有任何特殊名称。也没有任何其他整数类型的后缀。因此,如果你需要短文字或字节文字,它们必须被铸造:

The suffix does not have any special name in the Java Language Specification. Neither is there suffixes for any other integer types. So if you need short or byte literal, they must be casted:

byte foo = (byte)0;
short bar = (short)0;

在setLongValue(100L)中,您不必包含L后缀,因为在这种情况下为int literal会自动扩展为long。这在Java语言规范中称为扩展原语转换。

In setLongValue(100L) you don't have to necessarily include L suffix because in this case the int literal is automatically widened to long. This is called widening primitive conversion in the Java Language Specification.

这篇关于设置短值Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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