1

I was reading and reviewing the following site and had a question.

https://www.geeksforgeeks.org/angle-bracket-in-java-with-examples/

In the explanations below they show class definitions followed by <T> and then when actually implementing these classes they use different types such as or as the parameters. My question is: is the '' notation actually a defined syntax in Java? In particular, is the T a necessary thing in order to define a "Generic"? And then does it basically mean that the parameters can be of multiple different types? Also, if anyone can reword or explain in simpler terms what the meaning of a generic is that would be very helpful. Thanks.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
sgx
  • 1,265
  • 3
  • 19
  • 36
  • 2
    That site looks good; I'm not sure what further we could add. Yes, the `T` is a necessary thing - in their `class Test` example, it is used when declaring a local variable `T obj;`, and in the declaration of methods like `public T getObject()`. For each instance of `Test`, these take the types from the declared type at compile time - eg, `Test sObj` means sObj will have an instance variable `obj` of type `String`, and its `getObject()` method returns a `String`. – racraman Feb 13 '20 at 03:58
  • OK - so is my understanding correct that I described above? In other words, the T is an actual part of the syntax for Generics and it means that the paramter for the Class can be of variable type? – sgx Feb 13 '20 at 03:59
  • 1
    `` is the generic type. Maybe read the [official tutorial](https://docs.oracle.com/javase/tutorial/java/generics/index.html). – Elliott Frisch Feb 13 '20 at 04:00
  • 1
    Yes, the angle-brackets with one (or more) types is the syntax for generics. The T (to confirm, can be any identifier) serves as the placeholder for(in effect, is replaced by) the supplied type. Beyond that, I'm not sure what you mean "the parameter for the Class can be of variable type'. – racraman Feb 13 '20 at 04:02
  • Thanks Ole V.V. - I liked one part of one of the explanations where it stated: Specifying angular brackets after the class name means you are creating a temporary data type which can hold any type of data. – sgx Feb 13 '20 at 04:47
  • So after reading the documentation -one more question I had is - if you use Generics you are trying to specify "Type Parameters" rather than "Formal Parameters"...does this mean that you cannot pass values as a parameters when using generics and the paramter has to be an interface or class? – sgx Feb 13 '20 at 04:53
  • Ok - I maybe needed to rephrase that. I think I understood it couldn't be a value (or I mean hardcoded value), but it can be any type not just Class or Interface but also int for example – sgx Feb 13 '20 at 06:21

2 Answers2

5

The <T> is indeed a syntax defined by Java, but you can use whatever name you want to name a type, you don't need to use T, for example this is valid:

public class Box<MyType> {
    private MyType t;

    public void set(MyType t) { this.t = t; }
    public MyType get() { return t; }
}

But, stick with T or other common type names, as other people are already used to seeing those as the "generic types" so it makes reading your code simpler.

I recommend you read Java's Trail about Generics, where you can find the most commonly used type parameter names:

E - Element
K - Key
N - Number
T - Type
V - Value
S,U,V etc. - 2nd, 3rd, 4th types

As for "what the meaning of generics is", check this other page.

Daniel
  • 21,933
  • 14
  • 72
  • 101
1

It’s defined syntax since Java 5. You need a type parameter (one or more) for defining a generic. The type parameter needs not be called T, any Java identifier will do. A single uppercase letter is conventional, and T for type is what we often pick if we haven’t got a specific reason for some other letter.

The actual type parameter has to be a reference type. Values still always go into actual parameters in round brackets, not into type parameters (unlike in C++, for example). You can make a new ArrayList<Integer>(50), a list of Integer objects with an initial capacity for 50 of them.

That the actual type parameter has to be a reference type means that you can have a List<String>, a List<LocalDate> or a list of an interface type that you have declared yourself, even a List<int[]>. In the Java versions I have used (up to Java 10) the type parameter cannot be a primitive type (like int), but it can be an array type (like int[] or String[][], though often impractical). Allowing primitive types may come in a future Java version.

Link: Similar question: What does < T > (angle brackets) mean in Java?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161