• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

javabeat generics question doubt

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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??
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks chandra and manferd.....
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Chandra once again!!!

For such a superb Explanation!!!
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops

Tashi Rautela,

Sorry to confuse you. I corrected my post.


Thanks,
 
Tashi Rautela
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok... Thanks again

Thanks,
Tashi
 
It's just a flesh wound! Or a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic