• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in a question of Generics

 
Bijendra S. Rajput
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I took this question from javabeat site.

Question:-
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.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic