But the answer to this is compilation error Can someone explain me why? Is it because method 3 and method 5 are confusing the compiler as to which method to call??
debasmita
Manfred Klug
Ranch Hand
Joined: Jun 04, 2007
Posts: 377
posted
0
Hi debasmita,
let's have a look what the compiler generates behind the scenes:I think, no further comment is needed.
debasmita pattnayak
Ranch Hand
Joined: Apr 12, 2007
Posts: 94
posted
0
Hi Manfred, This was not clear for me. Why does the compiler behaves like this?? it is during the runtime in which the generic syntax has no role to play. I am bit confused... can you please elaborate on the same???
Thanks a lot..
Chandra Bhatt
Ranch Hand
Joined: Feb 28, 2007
Posts: 1707
posted
0
Hi Debasmita,
It is called type erasure. Compiler removes all the type information from the code before compilation is done. The type information that you study under generics is only to restrict the programmer to not be able to do unsafe operations.
For example, the parameterized type List<String> is translated to type List, which is called raw type. The same happens for the parameterized type List<Double>; it also appears as List in the byte code.
After translation by type erasure, all information regarding type parameters and type arguments has disappeared. As a result, all instantiations of the same generic type share the same runtime type, namely the raw type.
Example (printing the runtime type of two parameterized types):
[Debasmita]Is it because method 3 and method 5 are confusing the compiler as to which method to call??
This is called ambiguous method call. Because after type erasure every parameterized List becomes raw List. Compiler has not choice except generating method call ambiguity error.
Thanks,
debasmita pattnayak
Ranch Hand
Joined: Apr 12, 2007
Posts: 94
posted
0
thanks chandra and manferd.....
dhwani mathur
Ranch Hand
Joined: May 08, 2007
Posts: 621
posted
0
Hi!!Chandra
Thanks for such explanation it was realy easy to understand..... anyways i have one doubt please could you put some light on....
Ambigious method call
i am unable to understand this concept clearly.... it will be realy kinda of you..... Thanks in advance.....
Chandra Bhatt
Ranch Hand
Joined: Feb 28, 2007
Posts: 1707
posted
0
[Dhwani]: ... Ambiguous method call
I give you a very simple and prevalent example: Example #1
Compiler always searches for most specific method. It thinks, null can be given to String as well as StringBuffer. None of the method is more specific for this method call with null argument.
Example #2
meth(Number) is more specific to satisfy this method call.
Hi Chandra, I think in example #1 given by you,it wont give compiler error rather it will go to function which is taking string arg. the same is explained in the link which you provided above for discussion link.
Thanks, Tashi
Tashi Rautela
Greenhorn
Joined: Jun 27, 2007
Posts: 22
posted
0
i tried this code: class A { public void method(Object o) { System.out.println("Object"); } public void method(String s) { System.out.println("String"); } }
Class B { public static void main(String arg[]) { A a = new A(); a.method(null); } }
The above code gives "String" as output.....
Thanks, Tashi
Manfred Klug
Ranch Hand
Joined: Jun 04, 2007
Posts: 377
posted
0
Originally posted by Tashi Rautela: The above code gives "String" as output.....
Which is correct since Object is a superclass of String. Following an example with the expected error:
Chandra Bhatt
Ranch Hand
Joined: Feb 28, 2007
Posts: 1707
posted
0
Oops
Tashi Rautela,
Sorry to confuse you. I corrected my post.
Thanks,
Tashi Rautela
Greenhorn
Joined: Jun 27, 2007
Posts: 22
posted
0
Thanks to Chandra & Manfred to remove my confusion... But one more thing to ask "null" can go with Number also? I thought compiler is taking it as String only
Thanks, Tashi
Manfred Klug
Ranch Hand
Joined: Jun 04, 2007
Posts: 377
posted
0
Originally posted by Tashi Rautela: "null" can go with Number also? I thought compiler is taking it as String only
null is valid for any object reference.
Tashi Rautela
Greenhorn
Joined: Jun 27, 2007
Posts: 22
posted
0
ok... Thanks again
Thanks, Tashi
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.