Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic

 
sundar sankar
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Where can find some example/notes for creating generic class and generi method.
Thanks in advance
sankar
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java Tutorial ?
 
Olivier Croisier
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or the excellent Sierra & Bates SCJP book...
 
Quintin Stephenson
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Reet Goel
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 329
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Reet Goel
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15438
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>();".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic