• 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

Generics

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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) { }
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's used to tell that "T" is a generic type, not a real class.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 79518
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
And then the flying monkeys attacked. My only defense was this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic