File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generic" Watch "Generic" New topic
Author

Generic

sundar sankar
Ranch Hand

Joined: May 30, 2007
Posts: 42
Hi All,
Where can find some example/notes for creating generic class and generi method.
Thanks in advance
sankar
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Java Tutorial ?
Olivier Croisier
Greenhorn

Joined: Mar 14, 2006
Posts: 29
Or the excellent Sierra & Bates SCJP book...


SCJP 5.0 (100%)<br />SCWCD (getting ready...)
Quintin Stephenson
Ranch Hand

Joined: Nov 16, 2006
Posts: 40
The recommendations by Manfred and Olivier are both good.

Here's the one I used (seems to be similar to Manfred, but is in PDF format)
http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf


If at first you don't succeed, try, try again. If you don't try you have failed.
Reet Goel
Greenhorn

Joined: May 02, 2007
Posts: 14
Hi,

I have a class called Basket with the signature
public class Basket<E>

Now if I say
Basket b1 = new Basket < Orange > ();

then what is the significance of parameterizing it with Orange type, as I can add any type to it and what it returns is of type Object.

It works exactly the same way,Basket b2=new Basket(); works.

am i right?

Please reply.

Thanks.
Riya Pant
Greenhorn

Joined: Feb 17, 2006
Posts: 28
Hi,
You should change your methods accordingly to take benefit from your generic class declaration.Where you are writing Object as return type or parameter type in the Basket's methods, you should write E.
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

Originally posted by Reet Goel:

Now if I say
Basket b1 = new Basket < Orange > ();
...
It works exactly the same way,Basket b2=new Basket(); works.


The idea is that the reference you assign to is also parameterized:
Basket<Orange> b3 = new Basket<Orange>();

Now you can only add Oranges in the Basket.


SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
Reet Goel
Greenhorn

Joined: May 02, 2007
Posts: 14
Thanks Riya and Sergio.

I understand how Basket<Orange> basket=new Basket<Orange>(); works.

Actually, I specifically wanted to know if there's any difference between
Basket basket=new Basket<Orange>();
and
Basket basket=new Basket();

as I couldn't figure out any...

Please answer..

Thanks again
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

At runtime, there is no real difference between the two. Java generics work by type erasure: the compiler throws away the information about the parameterized type at compile time. Effectively generics are a compile-time only feature, just to check if you're not putting something in a collection that you shouldn't be putting in.

Doing something like this works without errors:

You will get a compiler error in the line where you are trying to put something in the basket:

Note: Test.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.


You really shouldn't be doing something like "Basket basket = new Basket<Orange>();".


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic