autoboxing

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

我是研究僧i 提交于 2019-11-26 11:00:03
问题 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

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

…衆ロ難τιáo~ 提交于 2019-11-26 10:42:05
问题 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

How java auto boxing/unboxing works?

余生颓废 提交于 2019-11-26 10:34:25
Since JDK 5.0, auto boxing/unboxing was introduced in java, the trick is simple and helpful, but when i started testing different conversions between wrapper classes and primitive types, i get really confused how the concept of auto boxing works in java, for example: Boxing int intValue = 0; Integer intObject = intValue; byte byteValue = 0; intObject = byteValue; // ==> Error After trying different cases ( short , long , float , double ), the only case which is accepted by the compiler is when the type of the value on the right of affectation operator is int . When i looked inside the source

Why does autoboxing make some calls ambiguous in Java?

为君一笑 提交于 2019-11-26 09:27:42
问题 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,

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

回眸只為那壹抹淺笑 提交于 2019-11-26 08:36:48
问题 This question already has answers here : Closed 8 years ago . 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? 回答1: 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

What code does the compiler generate for autoboxing?

烂漫一生 提交于 2019-11-26 08:27:09
问题 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? 回答1: 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

Why aren&#39;t Integers cached in Java?

眉间皱痕 提交于 2019-11-26 08:18:23
问题 I know there are similar posts on the topic, but they don\'t quite address my question. When you do: Integer a = 10; Integer b = 10; System.out.println(\"a == b: \" + (a == b)); This will (apparently) print true most of the time because integers in the range [-128, 127] are somehow cached. But: Integer a = new Integer(10); Integer b = new Integer(10); System.out.println(\"a == b: \" + (a == b)); Will return false . I understand that I am asking for new instances of an Integer, but since boxed

Java: What&#39;s the difference between autoboxing and casting?

亡梦爱人 提交于 2019-11-26 08:13:13
问题 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? 回答1: 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

Is autoboxing possible for the classes I create?

拈花ヽ惹草 提交于 2019-11-26 07:41:57
问题 Is there any way to use autoboxing for the classes I create? For example, I have this subclass of Number . public class UnsignedInteger extends Number { int n; public UnsignedInteger(int n) { if(n >= 0) this.n = n; else throw new IllegalArgumentException(\"Only positive integers are supported\"); } } Now, UnsignedInteger i = new UnsignedInteger(88); works perfectly fine, but is there any way to make this compile : UnsignedInteger i = 88; ? It won\'t for me. Thanks in advance! 回答1: In short,

Comparing boxed Long values 127 and 128

久未见 提交于 2019-11-26 06:58:33
问题 I want to compare two Long objects values using if conditions. When these values are less than 128 , the if condition works properly, but when they are greater than or equal to 128 , comparison fails. Example: Long num1 = 127; Long num2 = 127; if (num1 == num2) { // Works ok } Comparison on the code above works properly, but fails in the code below: Long num1 = 128; Long num2 = 128; if (num1 == num2) { // Does NOT work } Why is there a problem in comparing Long variables with values greater