Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A nagging question

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hello!

Could somebody please explain to me what the letter T in the following class definition means.






Thanks.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

The Sun Java Tutorial has a chapter on Generics that covers this.
 
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
Welcome to JavaRanch. Please use a meaningful subject line (instead of "A nagging question") when you start a topic.
 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here T can be replace with any class and you can use that generic somewhere in the class.

So whatever the operation performed based on generic should be the Class of type Number or of any sub classes of Number.

See the example below

e.g.



 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why we are using these type of Generic classes is nothing but we are providing the type safty for our class.

Here the T is just logical name.it means at run time you can pass any type of value into this class.

instead of hardcoading the value we are putting logical name.

The 'T' may any thing at runtime it may be Integer,String,Boolean....etc

Here the in the Test class

if you want to use the Test class for Integers you can use it.
Test<Integer> t1=new Test<Integer>(10);

if you want to use the Test class for Strings you can use it.
Test<String> t2=new Test<String>("praneeth");

if you placed the String in the place of 'T' you are allowed to insert only Strings.(just like its hardcoading)
so now my Test class is in a position to handle any type of Objects.

i request you please observe the bellow simple example.



 
Today you are you, that is turer than true. There is no one alive who is youer than you! - Seuss. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic