autoboxing

Autoboxing versus manual boxing in Java

♀尐吖头ヾ 提交于 2019-11-27 04:23:38
问题 Why is the second piece of code faster? Map<Integer, Double> map = new HashMap<Integer, Double>(); for (int i = 0; i < 50000; i++) { for (double j = 0.0; j < 10000; j++) { map.put(i, j); } } Map<Integer, Double> map=new HashMap<Integer, Double>(); for (int i = 0; i < 50000; i++) { for (double j = 0.0; j < 10000; j++) { map.put(new Integer(i), new Double(j)); } } 回答1: Autoboxing uses Integer.valueOf , which internally caches Integer objects for small integers (by default -128 to 127, but the

How do I convert Double[] to double[]?

一曲冷凌霜 提交于 2019-11-27 03:52:44
I'm implementing an interface that has functionality similar to a table that can contain an types of objects. The interface specifies the following function: double[] getDoubles(int columnIndex); Where I'm stumped is that in my implementation, I'm storing the table data in a 2D Object array ( Object[][] data ). When I need to return the values, I want to do the following (it is assumed that getDoubles() will only be called on a column that contains doubles, so there will be no ClassCastExceptions ): double[] getDoubles(int columnIndex) { return (double[]) data[columnIndex]; } But - Java doesn

When using == for a primitive and a boxed value, is autoboxing done, or is unboxing done

孤街浪徒 提交于 2019-11-27 03:48:40
The following code compiles (with Java 8): Integer i1 = 1000; int i2 = 1000; boolean compared = (i1 == i2); But what does it do? Unbox i1 : boolean compared = (i1.intvalue() == i2); or box i2 : boolean compared = (i1 == new Integer(i2)); So does it compare two Integer objects (by reference) or two int variables by value? Note that for some numbers the reference comparison will yield the correct result because the Integer class maintains an internal cache of values between -128 to 127 (see also the comment by TheLostMind). This is why I used 1000 in my example and why I specifically ask about

Java autoboxing rules

雨燕双飞 提交于 2019-11-27 02:01:41
问题 I am a java novice and so confused by the following example. Is it okay to think that "==" sign will compare the values between Integers and "autoboxed" Integers from int, and compare reference address between Integers? What about doubles and 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);

Java question about autoboxing and object equality / identity [duplicate]

孤街醉人 提交于 2019-11-27 01:51:46
问题 This question already has an answer here: Weird Integer boxing in Java 10 answers public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here int a1 = 1000, a2 = 1000; System.out.println(a1==a2);//=>true Integer b1 = 1000, b2 = 1000; System.out.println(b1 == b2);//=>false Integer c1 = 100, c2 = 100; System.out.println(c1 == c2);//=>true } } Why is b1 == b2 false and c1 == c2 true? 回答1: Read this. Java uses a

Why does autoboxing make some calls ambiguous in Java?

依然范特西╮ 提交于 2019-11-27 00:42:21
I noticed today that auto-boxing can sometimes cause ambiguity in method overload resolution. The simplest example appears to be this: public class Test { static void f(Object a, boolean b) {} static void f(Object a, Object b) {} static void m(int a, boolean b) { f(a,b); } } When compiled, it causes the following error: Test.java:5: reference to f is ambiguous, both method f(java.lang.Object,boolean) in Test and method f(java.lang.Object,java.lang.Object) in Test match static void m(int a, boolean b) { f(a, b); } ^ The fix to this error is trivial: just use explicit auto-boxing: static void m

Java Arrays.asList on primitive array type produces unexpected List type [duplicate]

落爺英雄遲暮 提交于 2019-11-26 23:14:00
Possible Duplicate: Arrays.asList() not working as it should? Apparently the return type of Arrays.asList(new int[] { 1, 2, 3 }); is List<int[]> . This seems totally broken to me. Does this have something to do with Java not autoboxing arrays of primitive types? The problem is that Arrays.asList takes a parameter of T... array . The only applicable T when you pass the int[] is int[] , as arrays of primitives will not be autoboxed to arrays of the corresponding object type (in this case Integer[] ). So you can do Arrays.asList(new Integer[] {1, 2, 3}); . Try: Arrays.asList(new Integer[] { 1, 2,

What code does the compiler generate for autoboxing?

谁都会走 提交于 2019-11-26 22:49:46
When the Java compiler autoboxes a primitive to the wrapper class, what code does it generate behind the scenes? I imagine it calls: The valueOf() method on the wrapper The wrapper's constructor Some other magic? You can use the javap tool to see for yourself. Compile the following code: public class AutoboxingTest { public static void main(String []args) { Integer a = 3; int b = a; } } To compile and disassemble: javac AutoboxingTest.java javap -c AutoboxingTest The output is: Compiled from "AutoboxingTest.java" public class AutoboxingTest extends java.lang.Object{ public AutoboxingTest();

Java: What's the difference between autoboxing and casting?

筅森魡賤 提交于 2019-11-26 22:16:09
This question is about "Why does autoboxing make some calls ambiguous in Java?" But reading through the answers, there are a number of references to casting and I'm not sure I completely understand the difference. Can someone provide a simple explanation? Boxing is when you convert a primitive type to a reference type, un-boxing is the reverse. Casting is when you want one type to be treated as another type, between primitive types and reference types this means an implicit or explicit boxing operation. Whether it needs to be explicit is a language feature. Both casting and boxing/unboxing

Autoboxing: So I can write: Integer i = 0; instead of: Integer i = new Integer(0);

六月ゝ 毕业季﹏ 提交于 2019-11-26 21:35:23
问题 Autoboxing seems to come down to the fact that I can write: Integer i = 0; instead of: Integer i = new Integer(0); So, the compiler can automatically convert a primitive to an Object. Is that the idea? Why is this important? 回答1: You kind of simplified it a little too much. Autoboxing also comes into play when using collections. As explained in sun's java docs: Collections can only hold object references, so you have to box primitive values into the appropriate wrapper class. ... When you