Question

AFAIK When Java automatically converts a primitive type into wrapper class object than its called autoboxing because primitive is boxed into wrapper class.

So is

int test = 3;
String str = String.valueOf(test);

counted as autoboxing?

Reason to ask?

I recently came across this question. Reason for which I think is autoboxing which was introduced in java 5(not present in Java 4).

Was it helpful?

Solution

No. Autoboxing only applies to primitives -> wrappers. Since there is no String primitive (or wrapper), String has nothing to do with autoboxing.

OTHER TIPS

Autoboxing is the automatic conversion of a primitive to its corresponding wrapper. While int is a primitive, String is not a wrapper.

According to http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes, String is not an object wrapper class.

Autoboxes only when there is an “impedance mismatch” between reference types and primitives

No.

If we look at the source code of valueOf method with int param

 public static String More ...valueOf(int i) {
      return Integer.toString(i, 10);
 }

calling ToString on Integer wrapper

A new string creating there in Integer wrapper toString()

  public static String More ...toString(int i, int radix) {

       if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
            radix = 10;

       /* Use the faster version */
         if (radix == 10) {
            return toString(i);
        }

       char buf[] = new char[33];
       boolean negative = (i < 0);
      int charPos = 32;

       if (!negative) {
            i = -i;
        }

        while (i <= -radix) {
            buf[charPos--] = digits[-(i % radix)];
           i = i / radix;
         }
        buf[charPos] = digits[-i];
        if (negative) {
             buf[--charPos] = '-';
        }

         return new String(buf, charPos, (33 - charPos));
     }

No, this wont be considered as Auto-Boxing. Auto-Boxing is between primitive and there Wrapper Classes. Please refer: http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html

From the Docs:

Converting a primitive value (an int, for example) into an object of the corresponding wrapper class (Integer) is called autoboxing. The Java compiler applies autoboxing when a primitive value is:

  • Passed as a parameter to a method that expects an object of the corresponding wrapper class.
  • Assigned to a variable of the corresponding wrapper class.

No this is not autoboxing.

Reason for which I think is autoboxing which was introduced in java 5(not present in Java 4).

Your logic is faulty.

The Question you linked to is about the change behaviour of String.valueOf(int) between Java 1.4.x and Java 1.5. Autoboxing was also added in Java 1.5. However, it is illogical to deduce that these two things are in any way connected.

Also, this is not an example of String interning. String.valueOf(int) did not intern the String either in Java 1.5 or in Java 1.4.x. (In the Java 1.4.x case, for some inputs, the method did return Strings that had previously been interned, but that was only because they were String literals.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top