Tag: 拳击

是否有内置的Java方法来装箱arrays?

是否有一种标准方法可以代替这种自定义方法? public static Byte[] box(byte[] byteArray) { Byte[] box = new Byte[byteArray.length]; for (int i = 0; i < box.length; i++) { box[i] = byteArray[i]; } return box; }

int.class在Java中是否等于Integer.class或Integer.TYPE?

让我们假设使用reflection检索Field的声明类型。 以下哪个测试将正确指示是否正在处理int或Integer ? Field f = … Class c = f.getDeclaringClass(); boolean isInteger; isInteger = c.equals(Integer.class); isInteger = c.equals(Integer.TYPE); isInteger = c.equals(int.class); isInteger = ( c == Integer.class); isInteger = ( c == Integer.TYPE); isInteger = ( c == int.class);

编译器错误:引用调用模糊

情况1 static void call(Integer i) { System.out.println(“hi” + i); } static void call(int i) { System.out.println(“hello” + i); } public static void main(String… args) { call(10); } 案例1的输出:hello10 案例2 static void call(Integer… i) { System.out.println(“hi” + i); } static void call(int… i) { System.out.println(“hello” + i); } public static void main(String… args) { call(10); } […]

整数缓存有多大?

类Integer具有缓存,缓存Integer值。 因此,如果我使用方法valueOf或inboxing,新值将不会被实例化,而是从缓存中获取。 我知道默认缓存大小为127但由于VM设置可以扩展。 我的问题是:在这些设置中缓存大小的默认值有多大,我可以操纵这个值吗? 这个值取决于我使用的是哪个VM(32位还是64位)? 我现在正在调整遗留代码,可能需要从int转换为Integer。 澄清:遵循我在Java源代码中找到的代码 private static class IntegerCache { static final int low = -128; static final int high; static final Integer cache[]; static { // high value may be configured by property int h = 127; String integerCacheHighPropValue = sun.misc.VM.getSavedProperty(“java.lang.Integer.IntegerCache.high”); if (integerCacheHighPropValue != null) { int i = parseInt(integerCacheHighPropValue); i = […]

整数包装类和==运算符 – 指定的行为在哪里?

Integer integer1 = 127; Integer integer2 = 127; System.out.println(integer1 == integer2);//true integer1 = 128; integer2 = 128; System.out.println(integer1 == integer2);//false 我发现它返回==(如果是)在-128 – 127的范围内,为什么有这样的规范?