简体   繁体   中英

Auto boxing and primitive types to match method signature

In version 1.5 , Java have introduced the concept of auto-boxing .

public interface SomeInterface {
    public void test(Integer val);
}

public class Main implements SomeInterface {

    /*The method signature gets different and compiler is asking to override 
    un-implemented methods*/
    public void test(int t) {

    }
}

Then why I am getting compile time error for overriding un-implemented methods, why above test method's arguments are not auto-boxed to match parent test method signature?

It's because the method in subclass is not override-equivalent with the one in super class. The super class method can take null as argument, while the subclass method can't (There's nothing about auto-boxing here).

Because Integer Not Equal to int

  • Integer is class
  • int is primitive type

So both methods have different argument types that's why you are not overriding the method but creating newer one in your class.

You can call method with autoboxing feature but you can not ovverride.

Overriding

An instance method in a subclass with the same signature (name, plus the number and the TYPE of its parameters) and return type ....


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.

Autoboxing and unboxing can happen anywhere where an object is expected and primitive type is available for example In method invocation where an object argument is expected, if you pass primitive, Java automatically converts primitive into equal value.

Read more: http://javarevisited.blogspot.com/2012/07/auto-boxing-and-unboxing-in-java-be.html#ixzz36Rhg91CB .

and here your class Main implements interface SomeInterface and this is the implementation of method not the calling of a method where autoboxing works.

This is conceptually impossible, because the Java runtime does not know a thing about boxing. The boxing feature was implemented on the Java compiler level , similarly to generics. As a matter of fact, the Java runtime does not consider a primitive int and a boxed Integer to have anything in common, even in the current Java 8 version.

Instead, whenever you make an assignment like:

Integer i = 42;

the Java compiler desugars this expression to

Integer i = new Integer(42);

which is then observed as above by the Java runtime. At the same time, the Java runtime considers

void foo(int i);
void foo(Integer i);

to be different methods which is why boxing would have been difficult to be implemented at the runtime level for reasons of backwards compatibility.

The simple answer to this is :

If 2 methods can be overloaded, it means, compiler considers the signatures to be different.

And, for Overriding a method, both the methods should be having the same signature.

Eg :

//Method overloading, compiler doesn't complain, which means "signatures are different",
//because one is expecting an object of Integer Class, and other is expecting a primitive value.
class A{
  public void a(Integer a){...}
  public void b(int a){...}
}

//Method Overriding, compiler complains, because "signatures are different!"
class A{
  public void a(Integer a){...}
}
class B extends A{
    @Override
    public void a(int a){...}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM