通常,编译器会生成执行装箱和拆箱的代码.但是,如果不需要盒装值,编译器会是什么?(Oracle标准)编译器是否足够智能以优化它?
看看这个方法:
public static void requireInRange(int index, Object[] array) {
if(index < 0 || index >= array.length)
throw new IndexOutOfBoundsException();
}
Run Code Online (Sandbox Code Playgroud)
唯一相关的信息是array.length,例如,将数组的每个值包装起来是没用的.喜欢这段代码:
int[] anArray = {3, 4, 2};
requireInRange(3, anArray);
Run Code Online (Sandbox Code Playgroud)
编译器是否会实际插入用于装箱数组的每个值的代码?
我真的无法理解为什么会发生以下情况:
Double d = 0.0;
System.out.println(d == 0); // is true
System.out.println(d.equals(0)); // is false ?!
Run Code Online (Sandbox Code Playgroud)
然而,这可以按预期工作:
Double d = 0.0;
System.out.println(d == 0.0); // true
System.out.println(d.equals(0.0)); // true
Run Code Online (Sandbox Code Playgroud)
我很肯定这与某种方式的自动装箱有关,但我真的不知道为什么0在使用==操作符和.equals调用时会有不同的方法.
这不是暗示违反equals合同吗?
* It is reflexive: for any non-null reference value * x, x.equals(x) should return * true.
编辑:
谢谢你的快速答案.我认为它的盒装方式不同,真正的问题是:它为什么装箱不同?我的意思是,这将是更直观,如果d == 0d不是d.equals(0d)非常直观和预期,但是如果d == 0它看起来像一个Integer是true不是"直觉" d.equals(0)也应该是真实的.
我很困惑为什么Integer和int可以在Java中互换使用,即使一个是原始类型而另一个是对象?
例如:
Integer b = 42;
int a = b;
Run Code Online (Sandbox Code Playgroud)
要么
int d = 12;
Integer c = d;
Run Code Online (Sandbox Code Playgroud) 任何人都可以解释为什么这个代码导致下面的输出?
@Test
public void testBooleanArray() {
Boolean[] ab = new Boolean[]{a, b};
a = new Boolean(true);
b = new Boolean(false);
for(Boolean x : ab) {
System.out.println(x);
}
}
Run Code Online (Sandbox Code Playgroud)
结果:
null
null
Run Code Online (Sandbox Code Playgroud)
数组ab是否应保存指向对象a和对象b的指针,因此输出:
true
false
Run Code Online (Sandbox Code Playgroud) Autoboxing相当可怕.虽然我完全理解之间的差异==和.equals我不能不帮助有后续错误的地狱了我:
final List<Integer> foo = Arrays.asList(1, 1000);
final List<Integer> bar = Arrays.asList(1, 1000);
System.out.println(foo.get(0) == bar.get(0));
System.out.println(foo.get(1) == bar.get(1));
Run Code Online (Sandbox Code Playgroud)
那打印
true
false
Run Code Online (Sandbox Code Playgroud)
他们为什么这样做?它与缓存的整数有关,但如果是这样的话,为什么它们不只是缓存程序使用的所有整数?或者为什么JVM始终不会自动拆箱到原始状态?
打印虚假或真假会更好.
编辑
我不同意旧代码的破坏.通过foo.get(0) == bar.get(0)返回true,您已经破坏了代码.
通过在字节代码中将int替换为int(只要从未赋值为null),就不能在编译器级别解决这个问题.
在某些情况下,方法需要基本类型double,并将Double对象作为参数传递.
由于编译器将传递的对象解包,这会增加内存使用量还是降低性能?
我知道自动解除拳击应该小心,因为未被装箱的引用可以为空.为什么自动装箱也被标记为警告?我在这里遗失了一些陷阱吗?
为了性能和安全性,我想实现一个固定大小的向量,它既是不可变的又是专用的(我需要快速的算术).我的第一个想法是使用@specialized注释(因为我需要整数和实数).
这是第一次尝试:
package so
class Vec[@specialized A] private[so] ( ary: Array[A] ) {
def apply( i: Int ) = ary(i)
}
Run Code Online (Sandbox Code Playgroud)
但是,当我分析生成的字节码时javap,我可以看到元素仍然是盒装的.例如:
public double apply$mcD$sp(int);
Code:
0: aload_0
1: iload_1
2: invokevirtual #33; //Method apply:(I)Ljava/lang/Object;
5: invokestatic #83; //Method scala/runtime/BoxesRunTime.unboxToDouble:(Ljava/lang/Object;)D
8: dreturn
Run Code Online (Sandbox Code Playgroud)
看起来数组并不是专门的,这似乎很愚蠢,因为数组专门用于JVM.
我还能做些什么来实现我的目标吗?
今天我一直在玩Eclipse Juno.来自Helios,这是一个很好的升级.一切正常,除了一个新的编译错误.
我们使用java.net框架'Fuse',我们调用以下方法:
ResourceInjector.get().inject(true, this);
Run Code Online (Sandbox Code Playgroud)
Eclipse告诉我们:
The method inject(Object[]) is ambiguous for the type ResourceInjector
Run Code Online (Sandbox Code Playgroud)
以下方法发生冲突:
inject(Object... components);
inject(boolean arg0, Object... arg1);
Run Code Online (Sandbox Code Playgroud)
它在Eclipse Helios中运行良好(使用Java 1.6.0.25),但现在它提供了编译错误,并且不想再运行了.在我们看来,这是Eclipse Juno中的一个错误,如果我们使用Maven进行构建是很好的建设......有人知道解决这个问题吗?
我是一个java新手,并且对以下示例感到困惑.是否可以认为"=="符号将比较Integers和int中的"autoboxed"整数之间的值,并比较整数之间的参考地址?
双打和0/0怎么样?
import edu.princeton.cs.introcs.*;
public class Autoboxing {
public static void cmp(Integer first, Integer second) {
if (first < second)
StdOut.printf("%d < %d\n", first, second);
else if (first == second)
StdOut.printf("%d == %d\n", first, second);
else if (first > second)
StdOut.printf("%d > %d\n", first, second);
else
StdOut.printf("%d and %d are incomparable\n", first, second);
}
public static void main(String[] args) {
cmp(new Integer(42), 43);
cmp(new Integer(42), new Integer(42));
cmp(43, 43);
cmp(142, 142);
Integer a0 = 1000;
int b0 = …Run Code Online (Sandbox Code Playgroud) autoboxing ×10
java ×9
eclipse ×2
boolean ×1
boxing ×1
caching ×1
double ×1
eclipse-juno ×1
equals ×1
integer ×1
performance ×1
scala ×1