wood burning stoves 2.0*
The moose likes Java in General and the fly likes declarations with <> operator with classes like S , T , K, V, M, B Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "declarations with <> operator with classes like S , T , K, V, M, B" Watch "declarations with <> operator with classes like S , T , K, V, M, B" New topic
Author

declarations with <> operator with classes like S , T , K, V, M, B

ulvi ugur
Ranch Hand

Joined: Aug 05, 2003
Posts: 46

Hi all,

Can anybody tell me what the below conventions are called and with which version of Java the are introduced :



or better with a question mark (?)



Thanks in advance,

Ulvi
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

Generics, Java 1.5


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
ulvi ugur
Ranch Hand

Joined: Aug 05, 2003
Posts: 46

Hi Bear,

I am actually using generics for collections to declare the content of it but what do these single letters mean ? Are they real classes or is it just a dummy identifier to be replaced with a real class ?

Cheers,

Ulvi
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

It's the generic type, and it indeed gets replaced with something "real" like Integer, String or whatever you want when you use the class.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051


these are called generics and introduced in java 1.5........
hava a look in chapter 7 of kathy book......scjp1.6...if you want to learn it.


SCJP6.0,My blog Ranchers from Delhi
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Generally
<T,V> or <T> is called as type parameter.
<?> is called as unbounded wildcard notation
bounded wildcard :
<? extends Class> is called as upper bounded wildcard notation
<? super Class> is called as lower bounded wildcard notation

hope it clear the generic terminology.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

The single letters are the type parameters of the generic class that they are specified in. Note that they don't have to be single letters; using single, upper-case letters for type parameters is just convention.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37907
    
  22
Look in the Java™ Tutorials: here, and here.
ulvi ugur
Ranch Hand

Joined: Aug 05, 2003
Posts: 46

Thanks guys, very helpful !
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37907
    
  22
You're welcome
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37907
    
  22
Seetharaman Venkatasamy wrote:Generally
<T,V> or <T> is called as type parameter.
Isn't that (<T>) called a formal type parameter, and when you write List<String>, then "String" is the actual type parameter? I think that's correct, but I'm not sure.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

For the exact definition:

Section 4.4 of the Java Language Specification describes type variables. You can use type variables in generic class, interface, method and constructor declarations as parameters in those declarations - so there they are called type parameters.

When you use a generic class or interface with a specific type, in other words when you fill in specific, existing types for the type arguments, you get a parameterized type. For example, List<String> is a parameterized type - you've filled in String for the type parameter T of the generic type List<T>. The Java Language Specification calls the type that you fill the parameter with the actual type argument.

The words "parameter" and "argument" mean the same (so "type parameter" and "type argument" also mean the same).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37907
    
  22
Thank you, Jesper. I suspect I got my nomenclature from Effetive Java by Bloch, and Bloch uses a different nomenclature from the Java Language Specification.
 
Consider Paul's rocket mass heater.
 
subject: declarations with <> operator with classes like S , T , K, V, M, B
 
Similar Threads
multidimensional arrays, references and values. Need help understanding...
I want to merge the two files into one of kind: by placing englishKeyword=English in format Russian
June Newsletter Puzzle
'a href' in firefox