aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt Abt Generic 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 Abt Generic" Watch "Doubt Abt Generic" New topic
Author

Doubt Abt Generic

Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643
What is difference between

Basket<Fruit> f1=new Basket<Fruit>(); //1
Basket f2=new Basket(); //2
Basket f2=new Basket<Fruit>(); //3



class Basket<E> {
private E element;
public void setElement(E x) {
element = x;
}
public E getElement() {
return element;
}
}
class Fruit {}

Line 1 is generic no doubt abt it.
Line 2 is non generic no doubt abt it also.
Is line 3 same as non generic (raw)?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
What's "Abt" while we are about it? Try our Use Real Words note.

For an introduction to Generics try the Generics Tutorial.

Look for "generic types" and "raw types".
[ July 21, 2006: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809


Line 3 is a raw type declaration as you can add any object in the f3.

f3.setElement(new Fruit()); legal
f3.setElement(new Dog()); legal

You can even add a dog into it. You will get a warning at the time of setting but no warning when you are getting object from Basket.

Naseem


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt Abt Generic
 
Similar Threads
doubt in Generics question from javabeat.net
Doubt About Generic
Generics doubt.
Generics Doubt
Generic doubt