Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the <TYPE> in java mean?

Tags:

java

syntax

I have seen declarations, interfaces and classes that go TYPE<CLASS>

What does this do/mean?

like image 523
tref95 Avatar asked Sep 29 '12 02:09

tref95


People also ask

What is the meaning of type in Java?

The idea is basically to make stronger type-safety in Java. So, a declaration like List<Integer> intList means intList has Integers in it. And if you try to put a, say, String -- it will throw compilation error. Follow this answer to receive notifications.

What are the types in Java?

The types of the Java programming language are divided into two categories: primitive types and reference types. The primitive types (§4.2) are the boolean type and the numeric types. The numeric types are the integral types byte , short , int , long , and char , and the floating-point types float and double .

What does class <?> Mean in Java?

What Does Class Mean? A class — in the context of Java — is a template used to create objects and to define object data types and methods. Classes are categories, and objects are items within each category. All class objects should have the basic class properties.

What is a type variable in Java?

Variable in Java is a data container that saves the data values during Java program execution. Every variable is assigned a data type that designates the type and quantity of value it can hold. A variable is a memory location name for the data. A variable is a name given to a memory location.


1 Answers

Without evidence, I believe you're talking about Java's Generics support...

Generics allow you to abstract over types

Before Java 5 it was difficult to provide classes that were capable of supporting multiple different types of Objects without having to code for each specific situation, so it was common for people to pass Object instead.

This leads to many difficult choices to make at runtime, you'd have to do a runtime check to see if it was possible to cast a given Object to a usable type...for example

List myIntList = new LinkedList(); // 1
myIntList.add(new Integer(0)); // 2
Integer x = (Integer) myIntList.iterator().next(); // 3    

Now, this is reasonably obvious, but if you were passed just a List, you'd have to check each and every element in the list for correctness...

But now, we can do this...

List<Integer> myIntList = new LinkedList<Integer>(); // 1'
myIntList.add(new Integer(0)); // 2'
Integer x = myIntList.iterator().next(); // 3'

This is a contract that basically says "This list only contains Integer type's of objects".

With generics you can construct a single class that is capable of handling multiple different data types or a family of data types (ie constraint the parameter so that it must be extended from a particular parent type).

Iterator<? extends Number> itNum;

Basically says, this will contain objects that inherit from Number, include Integer, Long, Double, Float...

Often in method and class decelerations you will see something similar to...

public class MyGenericClass<T> {...}

or

public class MyGenericClass<T extends MyBaseObject> {...}

This allows you to refer to T as if it were a concrete object type, for example...

public class MyGenericClass<T extends MyBaseObject> {
    private T value;
    public MyGenericClass(T value) {
        this.value = value;
    }
}

This allows the compiler (and JVM) to essentially "replace" the marker T with a concert type (okay, it's a little more complicated then that, but that's the magic)...

This allows to do things like...

... new MyGenericClass<MySuperObject>(new MySuperObject());
... new MyGenericClass<MySuperSuperObject>(new MySuperSuperObject());

And know that it will only ever accept the type of object I specify...

Have a read through the link in the first paragraph, I'm sure it can do more justice then I can ;)

like image 129
MadProgrammer Avatar answered Sep 27 '22 22:09

MadProgrammer