我有以下 Java 代码:
void foo(int a) { ... }
void bar() {
Long x = 42;
foo(x); // Compile error: "Method foo(int)
// is not applicable for the argument (long)"
foo((long) x); // Same as before
foo((int) x); // Compile error: "Cannot cast Long to int"
foo((int) (long) x); // OK, but strange and confusing
}
有更好的方法吗?
最佳答案
如果您有一个 Long
对象,则可以调用 intValue()
method获得一个int
。
关于java - 在java中自动装箱/将Long强制转换为int的更简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19122546/