views:

400

answers:

6

Hi All,

I am getting confused as and when i come across a generic method of this sort?

public static <T> T addAndReturn(T element, Collection<T> collection){
    collection.add(element);
    return element;
}

The reason being is that i could not understand why is required in this method?

Also, I would be greatful to you if you can explain the difference between generic methods and the methods that use generics syntaxes?

Thanks in advance.

Jegan

A: 

The alternative, using object instead of T, would cause the function to return object.

When it returns T, you can do something like:

addAndReturn(myElement, col).SomeMethodOfMyElement();

If addAndReturn returned object instead, you would have to use either

((MyElementType)addAndReturn(myElement, col)).SomeMethodOfMyElement();

which needs an explicit cast, or

addAndReturn(myElement, col);
myElement.SomeMethodOfMyElement;

which needs two statements instead of one.


EDIT: Now that your question has been formatted, I see that one of the parameters is Collection<T>. In that case, the generic syntax ensures that

addAndReturn(mySquare, collectionOfCircles);

returns a compile-time error instead of a run-time error.


EDIT: And just in case your question was about the <T> syntax rather than about the use of generics in general: The <T> tells the compiler that the T used in the method definition is not some class T but rather a placeholder for whatever class "fits" for a specific call of the method.

Heinzi
Thanks for the last EDIT comment. That what my question is all about.All i need to know is this.Does the following code snippets make any difference to the compiler? If so, how?1. public static <T> T addAndReturn(Collection<T> collection){ }2. public static T addAndReturn(Collection<T> collection){ }
Jegan
Sorry. i missed my formatting this time as well. Can you let me know how do i format my code in this?
Jegan
In the second variant, the compiler thinks that you are creating a function that returns class T. Literal "T", as in "class T { ... }". In the first variant, you are creating a function that works for *all* classes: If you pass an Integer and a Collection<Integer>, it will return an Integer.
Heinzi
A: 

That is a generic T is simply a placeholder for the more concrete type that is returned. In the case you supplied T is the type of element you are adding to the collection.

Collection<int> intCollection = new Collection();
int addedInt = addAndReturn(5, intCollection);
addedInt == 5;

Since they are all T rather than T1 or T2 they must be the same type of object as a single type is required obviously since the function adds a type to a generic collection

RHicke
As this is java, I hoped you meant Collection<Integer> :)
penpen
A: 

T is used for polymorphic reasons, if we may say so. T is any type of class Type.

That said, if you come to have a Customer class, for instance, you may use this above mentioned method to process your user type Customer class. T allows you to place in any type you wish.

T will allow you to use this generic method for your own user defined types, just like it will do for value types or any other types. T becomes the type specified in the element parameter.

Will Marcouiller
A: 

In java 5, this is known as generics. You can read more about them here. Basically, they provide the compiler a way to check the type of collections so you don't have invalid casts don't fail at runtime.

semaj
Thanks Semaj. I know that generics is a feature of java5. Also, i know how to use generics. But my question is why generic methods have got <T> along with their return type T.
Jegan
A: 

This method returns the same input element probably because implementers intended to use a Fluent Interface like way to provide a more readable API:

addAndReturn(myElement, col).doElementStuff().doOtherElementStuff();

Here you have a nice tutorial on Java generics that I find complete and enlightening.

bruno conde
+1  A: 
> public static <T> T addAndReturn(T
> element, Collection<T> collection){
>     collection.add(element);
>     return element; }

The <T> (in angle brackets) is known as the generic type parameter, whereas the T directly preceeding the method name is the return type. You might have a generic method that returns some other type than the generic type. Perhaps you want a method that adds an element to a collection (so an object of type T), and rather than returning the added object (which would also be of type T), you want it to return the index of that item in the collection, which would always be an int.

Example method signature where the return type is not the generic type:

public static <T> Integer addAndReturn(T element, Collection<T> collection)

Generic methods, like any other method, can have any return type, including the generic type itself, any other class type, any basic or inherent data type, or void. You may be confusing 2 unrelated parts of the method signature, thinking they are dependent, when they are not. They are only "dependent" in the sense that if you do use T as the return type, the return value's type is of the generic type you provide at the call site.

Samuel Meacham