0

I'm a little bit confused about this code, it is a test to understand the parametric class.

I have 2 classes, one is the Main class and the other is the parametric class. When I create a new Object with Integer as a parameter and I think into the constructor will call the print(Integer x) method, but it is not like this, instead java will call the print(Object o) method.

Someone of you know why it happened?

package classi.parametriche;

public class Contenitore <E> {
public E variabile;

public Contenitore(E value){
    variabile = value;
    System.out.println("Variabilie : "+variabile.getClass().toString());
    System.out.println("Variabile : "+value.getClass().toString());
    println(variabile);
}
public void println(String s){
    System.out.println("Stringa : "+ s);
}
public void println(Integer x){
    System.out.println("Int : " + x);
}
public void println(short x){
    System.out.println("short : " + x);
}
public void println(byte x){
    System.out.println("byte : " + x);
}
public void println(long x){
    System.out.println("long : " + x);
}
public void println(char x){
    System.out.println("char : " + x);
}
public void println(float x){
    System.out.println("float : " + x);
}
public void println(double x){
    System.out.println("double : " + x);
}
public void println(Object o){
    if (o != null){
        System.out.println("Object : " + o.toString());
    }else{
        System.out.println("null");
    }
}

}

public class ClassiParametriche {
    public static void main(String[] args) {
        Contenitore<Integer> c = new Contenitore <Integer>(42);

    }
}

this is the result :

 run:
 Variabilie : class java.lang.Integer
 Variabile : class java.lang.Integer
 Object : 42
Nick Vanderhoven
  • 2,768
  • 15
  • 26
  • 1
    Possible duplicate of [Overloading in Java and multiple dispatch](http://stackoverflow.com/questions/9759141/overloading-in-java-and-multiple-dispatch) – Smutje Nov 28 '16 at 14:45
  • 3
    The Generic's facility are available for compilation only. Once the java code is compiled into .class file the generics features e.g parameters to class, are erased i.e. At run time you have only Object. – Ankush G Nov 28 '16 at 14:48
  • 1
    Look into type-erasure. The generic `E` is only known as `Object` at runtime. – Reinstate Monica Nov 28 '16 at 14:48
  • 1
    This has nothing to do with type erasure, since the overloaded method used is chosen on compile time, not at run time. In compile time, we only know `E` is an `Object` or its subclass, so `Object` variant will be used. – Jiri Tousek Nov 28 '16 at 14:56

2 Answers2

0

This is because of type erasure and compile time binding.

The compiler decides at compile time which of the println methods is called. Because the field of your class is a generic with no defined super class the remaining type after type erasure is Object. This means the field variable is internally handled as an Object.

You can also check this post for details.

Community
  • 1
  • 1
manuel
  • 517
  • 6
  • 7
  • yeah I have understood it less the 5 minutes ago , thanks for your quick answer :) I was confuse because when first I controled the "type of class" i saw the name Integer and I didn't think that this method could belong to an Object – Alessandro Domeneghetti Nov 28 '16 at 15:41
-1

The answer is type-erasure. At run-time the class has no parameter type. Generics in Java is purely compile-time sugar. Once the code is compiled, it loses any knowledge of the associated parameter type. So when you pass the Integer into the constructor, the constructor is actually expecting a variable of type java.lang.Object

zcarioca
  • 153
  • 8
  • 1
    This has nothing to do with type erasure, since the overloaded method used is chosen on compile time, not at run time. – Jiri Tousek Nov 28 '16 at 14:57