• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics

 
sudharshan tettu
Ranch Hand
Posts: 126
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi
can anyone explain what is <T> here in the method signature , is that a return type ? am new to using generics

static <T> void fromArrayToCollection(T[] a, Collection<T> c) { for (T o : a) { c.add(o); // correct
}}

when to use and what is need for using that.


Thanks and regards
Sudharsan Tettu
 
Ganesh Gowtham
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sudarsan ,

Usage of <T> is custamisable for any object , not restricted object of any type.

example you will iterate over array a which have object of type T and adds in collection c .

ex1 : array a can have values of type String --> collection should be created to have String .

ex2 : array a can have values of type Perosn --> collection will have all persons from array a after calling fromArrayToCollection(...)

Hope it helps .
 
sudharshan tettu
Ranch Hand
Posts: 126
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi thanks for posting

am actually concerned about the <T> next to static

what is that specify?

static <T> void fromArrayToCollection(T[] a, Collection<T> c) { }
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's used to tell that "T" is a generic type, not a real class.
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use a generic type on either a class or a method - this is using it on a method. It means the method has been written to accept multiple argument types.

What it means in this case is roughly: "you can call fromArrayToCollection with any array and any Collection as the arguments, as long as the type of the array is the same as the type of the argument".

Does that make sense?
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at the Java™ Tutorials section, especially the bit about "generic methods . . ." That may help you
 
sudharshan tettu
Ranch Hand
Posts: 126
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
am really sorry to bug you back... let me post my question the other way...

what difference these two pieces of code make

static <T> void fromArrayToCollection(T[] a, Collection<T> c) { for (T o : a) { c.add(o); // correct
}}


and


static void fromArrayToCollection(T[] a, Collection<T> c) { for (T o : a) { c.add(o); // correct
}}


that is with and without <T> next to static .
 
Dave Lorde
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method without the <T> following the 'static' is a generic method in a generic class, where the class has been declared to use 'T' as a generic type. Because the class declares that it's using 'T', the method can just use it without declaring it.

The method with the <T> following the 'static' is a generic method in a class that has not declared 'T' as a generic type. You can have a generic method in a non-generic class. The <T> declares that the method will be using 'T' as a generic type. The method needs to declare this because the class doesn't.
 
Ganesh Gowtham
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sudarsan tettu wrote:am really sorry to bug you back... let me post my question the other way...

what difference these two pieces of code make

static <T> void fromArrayToCollection(T[] a, Collection<T> c) { for (T o : a) { c.add(o); // correct
}}


and


static void fromArrayToCollection(T[] a, Collection<T> c) { for (T o : a) { c.add(o); // correct
}}


that is with and without <T> next to static .



static void fromArrayToCollection(T[] a, Collection<T> c) { for (T o : a) { c.add(o); // correct
}}


wont complie at all..

if you have declared class like

public class Hello <T> {

static void fromArrayToCollection(T[] a, Collection<T> c) { for (T o : a) { c.add(o); // correct
}}

}


above code will complie
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic