File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in a question of Generics 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 "Doubt in a question of Generics " Watch "Doubt in a question of Generics " New topic

Doubt in a question of Generics

Bijendra S. Rajput
Ranch Hand

Joined: Sep 19, 2006
Posts: 41

I took this question from javabeat site.

Which ones of the following lines can be compiled without an error?

a)Basket b = new Basket();

b)Basket b1 = new Basket<Fruit>();

c)Basket<Fruit> b2 = new Basket<Fruit>();

d)Basket<Apple> b3 = new Basket<Fruit>();

e)Basket<Fruit> b4 = new Basket<Apple>();

f)Basket<?> b5 = new Basket<Apple>();

g)Basket<Apple> b6 = new Basket<?>();

Ans. a, b, c, f

But according to me, the answer should be a,c and f.
I did not get why ans b is also correct.
Can anyone through a light on it.

Thanks <br /> <br />Regards,<br />------------------------------<br />Bijendra S. Rajput<br />SCJP 1.5<br />------------------------------
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2398
Remember that generics are implemented through type erasure, so Basket<Fruit> is a Basket. When you use a raw type though you lose the type checking at compile-time.
I agree. Here's the link:
subject: Doubt in a question of Generics
It's not a secret anymore!