Is this really widening vs autoboxing?

In the first case, you have a widening conversion happening. This can be see when runinng the “javap” utility program (included w/ the JDK), on the compiled class: public static void main(java.lang.String[]); Code: 0: iconst_ 5 1: istore_ 1 2: iload_ 1 3: i2l 4: invokestatic #6; //Method hello:(J)V 7: return } Clearly, you see … Read more

What code does the compiler generate for autoboxing?

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 … Read more

Why does autoboxing make some calls ambiguous in Java?

When you cast the first argument to Object yourself, the compiler will match the method without using autoboxing (JLS3 15.12.2): The first phase (§15.12.2.2) performs overload resolution without permitting boxing or unboxing conversion, or the use of variable arity method invocation. If no applicable method is found during this phase then processing continues to the … Read more

Null values of Strings and Integers in Java

Your code makes use of two different additive operators. The first three lines use string concatenation, whereas the last one uses numeric addition. String concatenation is well-defined to turn null into “null”: If the reference is null, it is converted to the string “null” (four ASCII characters n, u, l, l). Hence there is no … Read more

Does autoboxing call valueOf()?

I first tought your question was a dupe of What code does the compiler generate for autoboxing? However, after your comment on @ElliottFrisch I realized it was different : I know the compiler behaves that way. I’m trying to figure out whether that behavior is guaranteed. For other readers, assume that “behaves that way” means … Read more

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

Java autoboxing/unboxing doesn’t go to the extent to allow you to dereference a primitive, so your compiler prevents it. Your compiler still knows myInt as a primitive. There’s a paper about this issue at jcp.org. Autoboxing is mainly useful during assignment or parameter passing — allowing you to pass a primitive as an object (or … Read more

tech