0

As far as I learn, with Integer example autoboxing usage is:

Integer iOb2 = 88; // auto-boxing
Integer iOb = new Integer(88) // is it auto-boxing ? I think no
                              // if it is auto-boxing what about above line?

The above code snippet works. However, could you answer the second line whether auto-boxing? With generics, I couldn't get the expected result.

// A very simple generic class. 
// Here, T is a type parameter that
// will be replaced by a real type
// when an object of type Gen is created.
class Gen<T> {
  T ob; // declare an object of type T

  // Pass the constructor a reference to 
  // an object of type T.
  Gen(T o) {
    ob = o;
  }

  // Return ob, which is of type T.
  T getob() {
    return ob;
  }
}

// Demonstrate the generic class.
class HelloWorld {
  public static void main(String args[]) {
    // Create a Gen reference for Integers. 
    Gen<Integer> iOb; 
    Integer iOb2;
    // Create a Gen<Integer> object and assign its
    // reference to iOb.  Notice the use of autoboxing 
    // to encapsulate the value 88 within an Integer object.
    //iOb = 88; //error
    iOb2 = 88;

    // Get the value in iOb. Notice that
    // no cast is needed.  The type is already known.
    //int v = iOb.getob();
    System.out.println("value: " + iOb2);

    System.out.println();

    // Create a Gen object for Strings.
    Gen<String> strOb = new Gen<String>("Generics Test");

    // Get the value of strOb. Again, notice
    // that no cast is needed.
    String str = strOb.getob();
    System.out.println("value: " + str);
  }
}

For this generic code, why isn't the integer value referred to type wrapper type which is Gen<Integer>? Whenas, it should be. Shouldn't it?

askque
  • 319
  • 2
  • 9
  • 2
    "is it auto-boxing ? I think no" You are correct. This is *boxing*, not *auto*-boxing. Auto-boxing is the compile-time substitution of this code (actually, it would be `Integer.valueOf(88)`, I think). – Andy Turner Feb 02 '16 at 17:04

3 Answers3

1

Integer iOb2 = 88 is implemented by the compiler as Integer iOb2 = Integer.valueOf(88). That is auto-boxing.

Integer iOb = new Integer(88) is just you constructing an Integer object. Not auto-boxing.

Auto-boxing is only for automatically converting primitive types to their equivalent Object versions, e.g. int to Integer. All auto-boxing operations are done using the valueOf() method, which was added in Java 5 for this particular purpose (exception for Boolean, where the method already existed).

Therefore, iOb = 88 is not valid, because 88 is an int and that is not assignment compatible with Gen<Integer>.

If you wrote iOb = new Gen<Integer>(88), then you would be causing auto-boxing before the object creation, because the constructor needs an Integer but you're supplying an int.

PROOF

To prove that auto-boxing uses valueOf(), I created the following code:

Boolean   a = true;
Character b = '1';
Byte      c = 1;
Short     d = 1;
Integer   e = 1;
Long      f = 1L;
Float     g = 1f;
Double    h = 1d;

Disassembling with the javap -c command produced (blank lines added for clarity):

 0: iconst_1
 1: invokestatic  #19                 // Method java/lang/Boolean.valueOf:(Z)Ljava/lang/Boolean;
 4: astore_1

 5: bipush        49
 7: invokestatic  #25                 // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
10: astore_2

11: iconst_1
12: invokestatic  #30                 // Method java/lang/Byte.valueOf:(B)Ljava/lang/Byte;
15: astore_3

16: iconst_1
17: invokestatic  #35                 // Method java/lang/Short.valueOf:(S)Ljava/lang/Short;
20: astore        4

22: iconst_1
23: invokestatic  #40                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
26: astore        5

28: lconst_1
29: invokestatic  #45                 // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
32: astore        6

34: fconst_1
35: invokestatic  #50                 // Method java/lang/Float.valueOf:(F)Ljava/lang/Float;
38: astore        7

40: dconst_1
41: invokestatic  #55                 // Method java/lang/Double.valueOf:(D)Ljava/lang/Double;
44: astore        8
Andreas
  • 154,647
  • 11
  • 152
  • 247
0

new Integer(88) isn't auto boxing - the closest usable term would be "boxing", but this is really just construction.

In your second code sample, it looks like you're attempting to autobox an int into your Gen object - that's not how auto boxing works.

Auto boxing only works with the specific types defined in the language - for example int can only autobox to Integer, and not to a type you define (like Gen)

Krease
  • 15,805
  • 8
  • 54
  • 86
0

Integer iOb = new Integer(88); is not auto-boxing, Integer takes an int parameter in one of its constructor overloads (see API).

The iOb2 = 88 assignment uses auto-boxing, as it's assigning an int literal to an Integer reference.

Mena
  • 47,782
  • 11
  • 87
  • 106