Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "T..." on Java?

Looking at the methods of the Array class on libgdx. I found this method:

public void addAll (T... array) {
    addAll(array, 0, array.length);
}

I had never seen that "T..." before, and it turns out that it's incredibly hard to search for "T..." either on Google or here on Stack Overflow. I think I understand generics, but the "..." is new to me.

What does that mean? So, for example, if T is String, then how would I use this method? Why would I use it? And how would that be different from using "T[]" instead?

like image 778
VIBrunazo Avatar asked Jan 23 '14 17:01

VIBrunazo


People also ask

What does \n and \t do in Java?

\t Insert a tab in the text at this point. \b Insert a backspace in the text at this point. \n Insert a newline in the text at this point. \r Insert a carriage return in the text at this point.

What is class T type in Java?

In java <T> means Generic class. A Generic Class is a class which can work on any type of data type or in other words we can say it is data type independent.

What does T stand for in programming?

T is a simple object-oriented programming language, modeled on Java. T supports only one primitive type, the integer type. T also supports reference types via the class Object, which is the root of the inheritance hierarchy. T supports only single-inheritance. T syntax is derived from Java syntax.

What is class t mean?

Delta Fare class T is a revenue fare/booking class of service that is marketed as Main Cabin on Delta Air Lines mainline and code share flights. This fare class is considered a deeply discounted main cabin fare class that is eligible for complimentary upgrades.


1 Answers

The T... is just a varargs parameter, where the element type happens to be T, the generic type parameter of the class.

The point is that you can call the method like this (assuming array is an Array<String>):

array.addAll("x", "y", "z");

which will be equivalent to

array.addAll(new String[] { "x", "y", "z" });

This can be used without generics too. For example:

public static int sum(int... elements) {
    int total = 0;
    for (int element : elements) {
        total += element;
    }
    return total;
}

Varargs parameters were introduced in Java 5.

like image 154
Jon Skeet Avatar answered Sep 21 '22 06:09

Jon Skeet