Tag: 拳击

为什么自动装箱被标记为警告?

我知道自动取消装箱应该小心,因为未装箱的参考可以为空。 为什么自动装箱也被标记为警告? 我在这里遗失了一些陷阱吗?

从Java原语转换为包装类

在将基元分配给包装类引用时,我对Java编译器的行为感到困惑。 请参阅下面的代码。 带注释的行不会编译。 我不明白为什么的逻辑: 一个byte可以分配给Byte或Short ,但不能分配给Integer或Long引用 short可以分配给Byte或Short ,但不能分配给Integer或Long引用 int可以分配给Byte , Short或Integer ,但不能分配给Long引用 long可以分配给Long ,但不能分配给Byte , Short或Integer 我看不到这种模式。 对此的任何见解都将非常有用。 谢谢。 Byte s5 = (byte)7; Short s6 = (byte)7; Integer s7 = (byte)7; // Does not compile Long s8 = (byte)7; // Does not compile Byte s9 = (short)7; Short s10 = (short)7; Integer s11 = (short)7; […]

拳击和拓宽

这两者有什么区别。 我知道Boxing正在将原始值转换为引用。 什么在扩大。 还应该做什么序列第一拳应该做或加宽应该做?

Java互操作性与Scalagenerics和装箱相关

假设我有这个Scala特征: trait UnitThingy { def x(): Unit } 提供Java实现很容易: import scala.runtime.BoxedUnit; public class JUnitThingy implements UnitThingy { public void x() { return; } } 现在让我们从一般特征开始: trait Foo[A] { def x(): A } trait Bar extends Foo[Unit] 上面的方法不起作用,因为单位x返回现在已装箱,但解决方法很简单: import scala.runtime.BoxedUnit; public class JBar implements Bar { public BoxedUnit x() { return BoxedUnit.UNIT; } } 现在假设我在Scala端有一个x定义的实现: trait […]

为什么有些语言需要拳击和拆箱?

这不是什么是装箱和拆箱的问题,而是为什么像Java和C#这样的语言需要呢? 我非常熟悉C ++,STL和Boost。 在C ++中,我可以很容易地写出这样的东西, std::vector dummy; 我有一些Java经验,但我真的很惊讶,因为我必须写这样的东西, ArrayList dummy = new ArrayList(); 我的问题,为什么它应该是一个对象,在谈论generics时,在技术上如此难以包含原始类型?

扩展java Integer缓存

由于缓存,一般建议使用Integer.valueOf(int)而不是new Integer(int) 。 在JDK 5+中,您应该使用valueOf因为Integer现在可以在-128和127之间缓存Integer对象,并且每次都可以将同一个Integer(0)对象交给您,而不是在全新的相同Integer对象上浪费对象构造。 如何扩大范围?

Java中的整数比较

Java中的整数比较很棘手,因为int和Integer行为不同。 我得到那个部分。 但是,正如此示例程序所示, (Integer)400 (第4行)的行为与(Integer)5 (第3行)不同 。 为什么是这样?? import java.util.*; import java.lang.*; import java.io.*; class Ideone { public static void main (String[] args) throws java.lang.Exception { System.out.format(“1. 5 == 5 : %b\n”, 5 == 5); System.out.format(“2. (int)5 == (int)5 : %b\n”, (int)5 == (int)5); System.out.format(“3. (Integer)5 == (Integer)5 : %b\n”, (Integer)5 == (Integer)5); System.out.format(“4. (Integer)400 […]

整数自动拆箱和自动装箱会产生性能问题吗?

我们目前正在使用x++;进行一些迭代和其他操作x++; 其中x是Integer而不是int 。 在我们的系统上的一些用户操作中可以重复操作,但没有像数学应用程序那样复杂或众多,每个用户事务最多可达10000次。 这种拆箱和后来的装箱是否会影响我们的性能一些明显的毫秒 ?

在知道类时创建(盒装)原始实例

我需要一个方法来返回提供的类类型的实例。 假设所提供的类型被限制为可以创建它们的“空”实例。 例如,提供String.class将返回一个空String,提供Integer.class将返回一个初始值为零的Integer,依此类推。 但是我如何动态创建(盒装)原始类型? 像这样? public Object newInstance(Class type) { if (!type.isPrimitive()) { return type.newInstance(); // plus appropriate exception handling } else { // Now what? if (type.equals(Integer.class) || type.equals(int.class)) { return new Integer(0); } if (type.equals(Long.class) // etc…. } } 迭代所有可能的原始类型的唯一解决方案,还是有更简单的解决方案? 请注意两者 int.class.newInstance() 和 Integer.class.newInstance() 抛出InstantiationException (因为它们没有nullary构造函数)。

将原语与包装器对象进行比较,其中==行为无法解释

我有一段代码需要我理解: public static void main(String[] args) { Character c = new Character(‘a’); Character cy = new Character(‘a’); char cx = ‘a’; System.out.println(c == cx); System.out.println(cx == cy); System.out.println(c == cy); } 输出: true true false 我无法理解为什么只有第三种说法失败了。 编辑:这个问题与.equals vs ==问题不同,因为这与原始与对象的比较有关。