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