51

I don't really understand the point of generics. What do they do, how do you use them?

From what I can tell, all they do is check return types at compile times instead of run times to avoid running the program before an error is thrown. Is this all they do?

for example:

public <Integer> int test() {
    return 'c'; //will throw error at compile instead of runtime
}

I was reading something about how generics are arbitrary, and you should only use capital letters? This is kind of confusing.

jgritty
  • 11,660
  • 3
  • 38
  • 60
switz
  • 24,384
  • 25
  • 76
  • 101
  • This is incorrect - you can't use a primitive as a generic. – duffymo Oct 19 '11 at 01:14
  • You do realize that regardless of whether or not there are generics, return types are always checked at compilation time. All you're doing here is confusing the programmer by returning a 'c' (which is ASCII value 143) instead of an obvious integer. – Chris Eberle Oct 19 '11 at 01:14
  • @duffymo I'm not actually sure if there's anything about that method signature that isn't incorrect or based on wrong assumptions. – millimoose Oct 19 '11 at 01:15
  • @Chris: that's what I thought, which is why I'm confused. I don't understand what Generics do. – switz Oct 19 '11 at 01:15
  • 2
    It's correct now that it's been changed. The first writing had – duffymo Oct 19 '11 at 01:15
  • @duffymo: Correct but not particularly useful because the type parameter `T` isn't used anywhere in the method signature. – millimoose Oct 19 '11 at 01:17
  • I changed it to `` now – switz Oct 19 '11 at 01:20
  • possible duplicate of [Java Generics](http://stackoverflow.com/questions/490091/java-generics) – Pablo Fernandez Oct 19 '11 at 01:22

3 Answers3

113

Generics allow you to customize a "generic" method or class to whatever type you're working with. For example, suppose you have a method that adds two numbers together. In order to work with the types themselves, you might have to create multiple versions of this method. For instance:

public int Add(int a, int b)

public double Add(double a, double b)

public float Add(float a, float b)

Generics allow you to create a single method that is customized for the type that invokes it.

public <T> T Add(T a, T b)

T is substituted for whatever type you use.

Mooncrater
  • 4,146
  • 4
  • 33
  • 62
Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • Okay, that's really clear, thanks! Now if I do something like `Stack = new Stack;` does that mean I have a stack that can only utilize `Location` objects? – switz Oct 19 '11 at 01:27
  • @Switz - Maybe. You have a stack of Location or Location derived objects. For instance, if you have a class called "MyLocation" derived from Location, you can add it to your stack. – Erik Funkenbusch Oct 19 '11 at 01:29
  • Thanks, really cleared it up! – switz Oct 19 '11 at 01:30
  • 2
    **Apart from the above answer: we use generics in ADT for type safety** – Premraj Jul 02 '15 at 11:47
  • 2
    Ironically generics are a poor choice for numeric types as mathematical operations are not part of an Interface. So this is kind of a bad example. – Novaterata May 25 '17 at 16:48
  • Generics are not templates. Primitives do not (yet) have common type in Java. So T could not be anything that then can be used for any of these types. There are wrappers and they all extend Number, but then you'd just be using BigDecimal internally. So this is just the worst example for generics. Maybe in the future Java will have a unified type system, but we are not there yet. – Claude Martin Aug 18 '18 at 19:38
  • This looks like C# code rather than Java code. – MC Emperor Jun 11 '21 at 10:28
12

Refer to the Fundamentals in Angelika Langer's FAQ – it's the best explanation of all things related to Java's generics you're likely to find. That said, the original primary goal of generics was to enable "typed" collections.

millimoose
  • 39,073
  • 9
  • 82
  • 134
5

The simplest way I can think of to explain generics is the good ol' copy-paste metaphore:

public <COPY> PASTE addTwoThings(PASTE a, PASTE b) {
    return a + b;
}

You specify a type (in the <COPY> portion) and java use this type throughout the code block, and it will make sure that the types are compatible. By doing it this way, you avoid having to write:

  • addTwoIntegers
  • addTwoStrings
  • addTwoMyObjects

etc. Don't confuse this for a preprocessor though, Java expects ONLY a type to be given.

Chris Eberle
  • 47,994
  • 12
  • 82
  • 119
  • So what will be the outcome here? – switz Oct 19 '11 at 01:20
  • 1
    The outcome is that if you specify "String" as the type (when you call it), then suddenly you've got a method that knows how to add two strings together. Likewise, if you specify an integer as the type, it will add two integers and return an integer. – Chris Eberle Oct 19 '11 at 01:22