autoboxing

Why can't the compiler/JVM just make autoboxing “just work”?

≯℡__Kan透↙ 提交于 2019-11-30 11:50:41
Autoboxing is rather scary. While I fully understand the difference between == and .equals I can't but help have the follow bug the hell out of me: 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)); That prints true false Why did they do it this way? It something to do with cached Integers, but if that is the case why don't they just cache all Integers used by the program? Or why doesn't the JVM always auto unbox to primitive? Printing false false or true

Why doesn't Java autoboxing extend to method invocations of methods of the autoboxed types?

匆匆过客 提交于 2019-11-30 07:48:37
I want to convert a primitive to a string, and I tried: myInt.toString(); This fails with the error: int cannot be dereferenced Now, I get that primitives are not reference types (ie, not an Object) and so cannot have methods. However, Java 5 introduced autoboxing and unboxing (a la C#... which I never liked in C#, but that's beside the point). So with autoboxing, I would expect the above to convert myInt to an Integer and then call toString() on that. Furthermore, I believe C# allows such a call, unless I remember incorrectly. Is this just an unfortunate shortcoming of Java's autoboxing

Why java does not autobox int[] to Integer[]

不羁的心 提交于 2019-11-30 07:10:54
问题 When I do the following, arrayList1 - contains one element and it is an int[] . arrayList2 - not compiling (Error : The constructor ArrayList<Integer>(List<int[]>) is undefined) arrayList3 - contains 7 elements and they are Integer objects Here's the code: int[] intArray = new int[]{2,3,4,5,6,7,8}; ArrayList arrayList1 = new ArrayList(Arrays.asList(intArray)); ArrayList<Integer> arrayList2 = new ArrayList<Integer>(Arrays.asList(intArray)); Integer[] integerArray = new Integer[]{2,3,4,5,6,7,8}

Do autoboxing and unboxing behave differently in Java and C#

穿精又带淫゛_ 提交于 2019-11-30 03:52:14
问题 I am manually converting code from Java (1.6) to C# and finding some difficulty with the behaviour of primitives (int and double). In C# it appears that almost all conversions happen automatically List<double> list1 = new List<double>(); // legal, C# double d0 = 3.0; list1.Add(d0); // legal, C# Double dd = 2.3f; // legal, C# list1.Add(dd); // legal, C# List<Double> list2 = new List<Double>(); // legal, C# double d1 = 3.0; list2.Add(d1); // legal, C# list2.Add(2.0); // legal, C# double d2 =

Equality comparison of `boolean` and `Object` allowed?

╄→尐↘猪︶ㄣ 提交于 2019-11-30 02:40:30
问题 The following code public class TestComparison { public static void main(String[] args) throws Exception { boolean b = true; Object o = new Boolean(true); System.out.println("comparison result: "+ (o == b)); // Eclipse complains about this expression } } compiles without errors with javac V1.7.0_15, and prints "false" when run. However, Eclipse Juno complains about "Incompatible operand types Object and boolean". Apparently javac autoboxes the primitive boolean b , and then compares o and

Why can't the compiler/JVM just make autoboxing “just work”?

别说谁变了你拦得住时间么 提交于 2019-11-29 18:08:34
问题 Autoboxing is rather scary. While I fully understand the difference between == and .equals I can't but help have the follow bug the hell out of me: 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)); That prints true false Why did they do it this way? It something to do with cached Integers, but if that is the case why don't they just cache all Integers

Overriding and return type compatibility

半世苍凉 提交于 2019-11-29 14:43:17
The following compiles without any problem boolean flag = true; Boolean flagObj = flag; Now imaging the following scenario interface ITest{ Boolean getStatus(); } class TestImpl implements ITest{ public boolean getStatus(){ // Compile error: return type is incompatible return true; } } My question is about the compile error at the mentioned line. My Interface mentions return type as Boolean but the implemented method returns boolean ( the literal ) My question is, if Boolean and boolean are compatible then why the compiler is complaining ? Doesn't the autoboxing apply here ? You can only

Kotlin boxed Int are not the same

血红的双手。 提交于 2019-11-29 14:42:09
Please help me understand this piece of code in the kotlin docs:- val a: Int = 10000 print(a === a) // Prints 'true' val boxedA: Int? = a val anotherBoxedA: Int? = a print(boxedA === anotherBoxedA) // !!!Prints 'false'!!! Now, I understand that first int a = 10000 then in the next line it is comparing it using === . Now the question is why when it assigned boxedA=a , it checked if it is null using int? . Can it just be written like this:- val boxedA: Int=a Please if I'm understanding it the wrong way, someone guide to check the right place or explain it a bit for me. when it assigned boxedA =

The expression of type x is boxed into X?

白昼怎懂夜的黑 提交于 2019-11-29 13:46:46
I'm a little confused by a warning that my Eclipse IDE is currently writing next to every expression where types are autoboxed or autounboxed: The expression of type x is boxed into X The expression of type X is unboxed into x Is this a warning I should react on? I thought autoboxing was a Java language feature - but now I seem to get warnings everytime this feature is used. I don't think Eclipse does this by default (mine does not), but you can turn it on or off using Preferences > Java > Compiler > Errors/Warnings > Potential programming problems > Boxing and Unboxing Conversions. It should

autoboxing of numeric literals : wrapper initialization vs passing method arguments inconsistency

北城以北 提交于 2019-11-29 10:33:07
Please consider 2 cases: //1 Short s = 10; //obviously compiles //2 takeShort(10); //error - int is not applicable //where: static void takeShort(Short s) {} I assume that case 1 is changed by compiler to : short _temp_s = 10; Short s = Short.valueOf(_temp_s); Could you please explain what compiler is trying to do in case 2, so it does not compile ? If it is not trying to apply autoboxing as it does in case 1, then why ? EDIT Reference to JSL in johnchen902 answer explains compiler's behaviour. Still not exactly clear why JLS does not support "A narrowing primitive conversion followed by a