Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Generic

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Where can find some example/notes for creating generic class and generi method.
Thanks in advance
sankar
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java Tutorial ?
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or the excellent Sierra & Bates SCJP book...
 
Ranch Hand
Posts: 44
1
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>();".
 
Would you turn that thing down? I'm controlling a mind here! Look ... look at the tiny ad ...
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic