Java: Experimenting with generics -
lastly experimenting generics little bit. came piece of code:
public class test { static <t> void f(t x) { x = (t) (integer) 1234; system.out.println(x); } public static void main(string[] args) { f("a"); f(1); f('a'); f(1.5); f(new linkedlist<string>()); f(new hashmap<string, string>()); } } i ran , got output:
1234 1234 1234 1234 1234 1234 with no exceptions! how possible?
it's because of type erasure (a lot has been written this, google term). after compiling f byte code method might this:
static void f(object x) { x = (object) (integer) 1234; system.out.println(x); } so system.out.println call tostring method on object x - , in case integer.tostring().
Comments
Post a Comment