I encountered a very interesting question where it was required to identify examples of autoboxing:
Code: Select all
Long l = Long.valueOf(200)
Integer i = 10;
Integer getValue() { return 2; }
Long getValue() { return 2; }
System.out.println(2 + "");
Code: Select all
System.out.println(2 + "");
This explanation means thatWhenever only one operand of the + operator is a String, the other operand is converted into a String using a string conversion. If the other operand is a numeric primitive type, then it is first converted to a reference type using the boxing conversion and then the boxed reference is used to produce a String. Thus, in this case, 2 will first be boxed into an Integer object and then the Integer object will be used to produce the String "2", which will then be concatenated with "". Hence, this is also a valid example where autoboxing occurs. Note that, no autoboxing occurs in System.out.println(2); because the println(int) method is invoked in this case.
Code: Select all
2
Code: Select all
Integer
Code: Select all
Integer
Code: Select all
String
To confirm or disprove this statement, we need to write code that contains all of the above cases, compile the class using the command:
Code: Select all
$ javac Test.java
Code: Select all
$ javap -c Test
Code: Select all
public class Main {
public static void main(String[] args) {
Long l = Long.valueOf(200);
Integer i = 10;
System.out.println(2 + "string");
// Not compile:
// String s = (String) (1 + 2);
}
static Integer getInteger() {
return 2;
}
static Long getLong() {
// Not compile:
// return 2;
return 2L;
}
}
Code: Select all
PS W:\testspace\untitled> javap -c W:\testspace\untitled\build\classes\java\main\l3\Main.class
Compiled from "Main.java"
public class l3.Main {
public l3.Main();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: ldc2_w #7 // long 200l
3: invokestatic #9 // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
6: astore_1
7: bipush 10
9: invokestatic #15 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
12: astore_2
13: getstatic #20 // Field java/lang/System.out:Ljava/io/PrintStream;
16: ldc #26 // String 2string
18: invokevirtual #28 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
21: return
static java.lang.Integer getInteger();
Code:
0: iconst_2
1: invokestatic #15 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
4: areturn
static java.lang.Long getLong();
Code:
0: ldc2_w #34 // long 2l
3: invokestatic #9 // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
6: areturn
}
PS W:\testspace\untitled>
1. Initializing the Long variable:
-
Code: Select all
ldc2_w #7
Code: Select all
200L
Code: Select all
long
-
Code: Select all
invokestatic #9
Code: Select all
Long.valueOf(long)
Code: Select all
Long
-
Code: Select all
astore_1
Code: Select all
Long
2. Initializing the Integer variable:
-
Code: Select all
bipush 10
Code: Select all
10
-
Code: Select all
invokestatic #15
Code: Select all
Integer.valueOf(int)
Code: Select all
Integer
-
Code: Select all
astore_2
Code: Select all
Integer
3. String concatenation and printing:
-
Code: Select all
getstatic #20
Code: Select all
System.out
Code: Select all
PrintStream
-
Code: Select all
ldc #26
Code: Select all
"2string"
-
Code: Select all
invokevirtual #28
Code: Select all
println(String)
4. Returning an Integer from a method:
-
Code: Select all
iconst_2
Code: Select all
2
-
Code: Select all
invokestatic #15
Code: Select all
Integer.valueOf(int)
Code: Select all
int
Code: Select all
Integer
-
Code: Select all
areturn
5. Returning a Long from a method:
-
Code: Select all
ldc2_w #34
Code: Select all
2L
Code: Select all
long
-
Code: Select all
invokestatic #9
Code: Select all
Long.valueOf(long)
Code: Select all
long
Code: Select all
Long
-
Code: Select all
areturn
These examples show how Java uses
Code: Select all
valueOf
It’s also clear that the concatenation of
Code: Select all
2 + "string"
Code: Select all
"2string"
Code: Select all
ldc
In other words, converting a number to a string or joining it with a string (concatenation) is not autoboxing, because
Code: Select all
String