-2

I am hoping somebody can explain to me what a <T> object is, just a little confused on what it is/what it contains. Thank you

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 2
    https://docs.oracle.com/javase/tutorial/java/generics/ – Radiodef Apr 12 '16 at 02:27
  • If you Google [`Java less than T greater than`](https://www.google.com/search?q=java+less+than+t+greater+than), the answer to your question actually does come up. Granted, most of the results are about comparison operations, but an explanation of generics is still in the first few hits. Don't be afraid to make silly-sounding searches! – user2357112 May 24 '16 at 17:09

1 Answers1

1

It's called a "Generic", and your best bet is probably to google that term. That being said, it's essentially a type that is dependent on another type, in a flexible way.

The easiest example is a List: You very likely care what a List is of, and don't just want to put cars, frogs, and everything else in the same list. That being said, you also don't really need to have CarList, FrogList, EverythingElseList, all with their own implementations, because the code will be pretty much exact. Generics let you do this with List<Car>, List<Frog>, etc: The code is the same, but once you declare something as a List<Car>, you can only ever add Cars to it, and you know that whatever you get out of it will be a Car.

In general, at least some of the methods of a Generic class will have either arguments or a return type of "<T>". That means that those methods refer to whatever Type the <T> of the class is.

Edward Peters
  • 3,623
  • 2
  • 16
  • 39