为什么Java API会使用int,何时short甚至byte是足够的?
示例:DAY_OF_WEEK类中的字段Calendar使用int.
如果差异太小,那么为什么存在这些数据类型(short,int)?
我想了解以下代码:
public class A {
private void doTask() {
doInt(99);
}
public short doInt(short s) {
return 100;
}
}
Run Code Online (Sandbox Code Playgroud)
代码在"doInt(99)"行上给出编译器错误.
The method doInt(short) in the type A is not applicable for the arguments (int)
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么它会给我错误.
嗨,我是新来的,这是我的第一个问题:我有一个带有简单算术运算符的代码。但它不会工作:
int a = 10;
short premennaTypuShort = (short)a; /*retyped variable a into short type and entered into new variable*/
premennaTypuShort = premennaTypuShort - 7;
/*now i am trying to decrease the number but eclipse wrote that cannot convert from int to short.*/
Run Code Online (Sandbox Code Playgroud)
我正在尝试减少指定为 short 的数字,但 eclipse 写的无法从 int 转换为 short。我不明白为什么。那么问题出在哪里呢?我该如何修复这个错误?
我正在尝试调用一个需要短值的函数。以下作品:
i.setDamage((short) 10);
Run Code Online (Sandbox Code Playgroud)
然而,这并不:
i.setDamage(10S);
Run Code Online (Sandbox Code Playgroud)
根据我正在使用的 IDE,这应该可以工作。为什么不呢?我正在使用 Maven 和 Java 7。