wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes javabeat generics question doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "javabeat generics question doubt" Watch "javabeat generics question doubt" New topic
Author

javabeat generics question doubt

debasmita pattnayak
Ranch Hand

Joined: Apr 12, 2007
Posts: 94
I couldnt understand the following question:


I thought the answer to be method 3.

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
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
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
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):



prints: runtime type of ArrayList<String>: class
java.util.ArrayList runtime type of ArrayList<Double> : class java.util.ArrayList

Thanks,


cmbhatt
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
[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
thanks chandra and manferd.....
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
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
[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.

For more clear understanding with example, see this link:
Danchisholms question discussion


EDIT: POST Edited, overloaded methods are meth(String arg) and
meth(StringBuffer arg)

Thanks,
[ July 23, 2007: Message edited by: Chandra Bhatt ]
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
Thanks Chandra once again!!!

For such a superb Explanation!!!
Tashi Rautela
Greenhorn

Joined: Jun 27, 2007
Posts: 22
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
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
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
Oops

Tashi Rautela,

Sorry to confuse you. I corrected my post.


Thanks,
Tashi Rautela
Greenhorn

Joined: Jun 27, 2007
Posts: 22
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
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
ok... Thanks again

Thanks,
Tashi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: javabeat generics question doubt
 
Similar Threads
compile time error - same erasure ?
Regarding generics
generics question
Generic Question in JAVA Beat
Generics problem