public class G<x> { x i; } public class E { public static void main(String[] args) { G<Integer> b1 = new G<Integer>(); G<String> b2 = new G<String>(); b1.i = 50; b2.i = "start"; System.out.println(b1.i); System.out.println(b2.i); } } How this case is different from the other case given below
public class G<x> { x i; } public class E { public static void main(String[] args) { G b1 = new G(); G b2 = new G(); b1.i = 50; b2.i = "start"; System.out.println(b1.i); System.out.println(b2.i); } } I know While you are making the Object of G class we have to define the type argument for generics but without passing the type argument it will work ..The output will be shown. So why my teacher says that Type argument is important though the code will run without it also.
There is a difference in both cases. In first case we are passing an integer type argument through the reference variable b1 and String type argument through the b2 reference variable but in the second case we are not doing this. And by not doing this In second case the data type will be object type. Both code will give you same answer, but my teacher says that you have to use always 1case .So my question was why he said so because both code will give you same answer so why cant we use 2 case