If int does not inherit from Object, then why is "String.format (String, Object ...)" compiled with int?

I read this post: Is int an object in Java? ...

The post claims to int

not inherit from Object

. If so, why does the code below compile without any error? Given that int

not Object

, but the method signature format()

is public static String format(String format, Object... args)

as shown in the documentation: javadoc for string

public class Testing {
    public static void main(String[] args) {
        int integer = 7;
        String str = String.format("%03d", integer);
        System.out.println(str);
    }
}

      

I also read about "Autoboxing". What does it mean? Are all primitives replaced with appropriate ones Object

before compilation? If so, is there any memory advantage when using a large array int

( int[]

) over Integer

( Integer[]

)? Similar arguments follow double, etc.

Any ideas are appreciated.

+3


source to share


3 answers


When called String.format("%d",myInt)

, it is myInt

automatically (and implicitly) wrapped in an instance Integer

that extends Object

, so it compiles.

As far as arrays are concerned, the conversion from primitiveType[]

to WrapperClass[]

is not automatic for some reason. If you try to use a primitive type array where a wrapper class array is expected, it will result in a compilation error.



Usage Integer

creates an overhead compared to usage int

because you need to assign and store links. However, this overhead is limited when using values Integer

between -128 and 127 because these values ​​are concatenated (meaning that all instances Integer

wrapping a value in that span point to an unambiguous reference).

+3


source


This is caused by Autoboxing .

Here's a small snippet from the linked Java documentation that explains it better than I could:

Autoboxing is an automatic conversion that the Java compiler makes between primitive types and their corresponding object wrapper classes. For example converting int to Integer, double to Double, etc. If the conversion goes the other way, it is called unboxing.

Here's the simplest example of autoboxing:

Character ch = 'a';

      



The rest of the examples in this section use generics. If you are unfamiliar with generics syntax, see the General (Updated) tutorial.

Consider the following code:

List<Integer> li = new ArrayList<>();
for (int i = 1; i < 50; i += 2)
    li.add(i);

      

Although you add int values ​​as primitive types rather than Integer objects, li, the code compiles. Since li is a list of Integer objects, not a list of int values, you might wonder why the Java compiler doesn't throw a compile-time error. The compiler doesn't generate an error because it creates an Integer object from i and adds an object to li. Thus, the compiler converts the previous code to after execution:

List<Integer> li = new ArrayList<>();
for (int i = 1; i < 50; i += 2)
    li.add(Integer.valueOf(i));

      

+5


source


Autoboxing is help from the compiler that automatically compiles something like

foo(i);

      

in

foo(Integer.valueOf(i));

      

when it foo()

takes an argument of type Object and you pass a primitive type to it (int in this case). It just makes it easier to enter code and read code.

And what's going on here. The method String.format()

expects objects as an argument. You are passing a primitive type to it, so the compiler will automatically add it to Integer for you.

+3


source







All Articles