• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generic  RSS feed

 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For simplicity, we first consider the signatures of the protected data and several
of the methods of the Association class, declared using parameterized types:


At the time of their declaration, parameterized class name are followed by a list
of comma-separated type parameters in angle brackets.


From:

Data Structures in Java for the Principled Programmer

I can't understand that code.Please explain about this:


What is "parameterized class"? Does class accept parameter like method?

Please what the generic is in JAVA.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generic classes are a big topic. The best examples are the collection classes. For example, we've got an ArrayList class that we can put anything in. But we don't have any type-safety with that. Generics allow us to create ArrayLists that take only a specific type. For example:
There's a lot more to them than that, though. The Java Tutorials have a lot of information on them.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A parameterized class is a Class that has parameters, hence ArrayList<T> is a parameterized class due to the parameter 'T'
When using a parameterized class, you specify what type will be represented by 'T' and this influences the behaviour of the rest of that instance.
When I'm dealing with Generics I tend to use the word 'of'
List<Integer> is a List of Integers
Comparator<String> is a Comparator of Strings (ie it compares Strings)

There are some common parameterized types that are used to indicate the intended use, but keep in mind all of them represent a Class type (or interface or enumeration or Exception)
T for Type
K for Key
V for Value (and others but I forget off the top of my head)

So Class<T> is a class of some type
Map<K,V> is a map of keys and values.

As stated, Generics is a large topic but it allows you to define the behaviour of some code without needing to specify (or limit) what type that behaviour acts on.
eg to define an interface that is able to load and save some basic type, you can define


This is not too useful, but not too threatening either. An IntegerSaver could then be

and the only difference is that 'Integer' replaces the parameterized type 'T'

The entire Collections framework has been reverse engineered to support Generics so that you now work with a Collection of a specific type and can be sure that all operations will support that type and the Collection (if used correctly) will only contain and return that type.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A parameterized class is a Class that has parameters, hence ArrayList<T> is a parameterized class due to the parameter 'T'
When using a parameterized class, you specify what type will be represented by 'T' and this influences the behaviour of the rest of that instance.


May you give me example about it?
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you taken a look at the Generics tutorials, as Matthew suggested?
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. But It is not clear.Friends in forum explain better.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about it isn't clear? They show examples, don't they? Is there something you don't understand about the examples?

Write down what you don't understand, and we'll help you.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!