GenericJam
GenericJam

Reputation: 3015

What does <> do in Java?

I know if you want a list (for example) you create it like:

List<String>

If you want to create a generic type of list you could do it like:

MyList<T>

So is the only thing <> does is to couple an object with a container or list? Does it have other uses? What does it actually do? Was reading on another post how putting static methods in generic types is a bad thing for type safety, so is this bad code?

public class LinkList<T> {

private final T t;
private final LinkList<T> next;

public LinkList(T t, LinkList<T> next){
    this.t = t;

    this.next = next;
}
// Creates list from array of T
public static <T> LinkList<T> getList(T[] t){
    if(t == null){
        return null;
    }
    LinkList linkList = null;
    for(int i = t.length-1; i >= 0; i--){
        linkList = new LinkList(t[i], linkList);
    }
    return linkList;
}

public T element() {
    return t;
}

public LinkList<T> getNext() {
    return next;
}

}

Upvotes: 6

Views: 27008

Answers (4)

ameed
ameed

Reputation: 1160

<> is used for a cool feature called generics.

Before Java 5, generics did not exist. Collections like ArrayList returned and manipulated Objects. The problem with this is when you know that you will only store Strings, for example. But if you are working with Objects in all your classes, not only do you have to use annoying casting (String blah = (String) list.get(9);), but if you make an error and put an Integer in your list, your program will ClassCastException and burn.

Java 5 solved this with generics, so now you can say ArrayList<String> to say that you will only use Strings in this ArrayList. But what if you need to make ArrayList<Supercalifragilisticexpealidocious>? Obviously, typing that is not a pleasant experience, especially when you have to type

ArrayList<Supercalifragilisticexpealidocious> supercaliList = new ArrayList<Supercalifragilisticexpealidocious>();

to declare one of these. Moreover, it could lead to typos, especially with a type parameter this size; at some point, you are bound to mistype a letter or two and make your program cannot find symbol and burn--or worse, silently use the wrong class and cause logic errors.

Java 7 introduces the plain <> syntax. It is called the diamond operator and makes it so you don't have to retype the type parameter (the thing inside the <>) on the right side of the assignment operator. Therefore,

ArrayList<Supercalifragilisticexpealidocious> supercaliList = new ArrayList<Supercalifragilisticexpealidocious>();

becomes

ArrayList<Supercalifragilisticexpealidocious> supercaliList = new ArrayList<>();

which means a lot less time retyping Supercalifragilisticexpealidocious.

Hope this helps!

Upvotes: 3

La bla bla
La bla bla

Reputation: 8708

When you put <> and a type inside, it is used in order to convert what would be a potential runtime exception, into a compilation error.

Take this code for example, without generics

ArrayList stringList = new ArrayList();
stringList.add("string");
stringList.add(3.4);
String s = (String) stringList.get(1);

// THIS WOULD COMPILE AND PRODUCE A RUNTIME ERROR, COMPARING String TO double.

If you add generics, you could could find these bugs when writing it.

consider the following code:

ArrayList<String> stringList = new ArrayList<String>(); // Since Java 7 you can write - new ArrayList<>()
stringList.add("string"); // OK
stringList.add(3.4); // Would not compile!

This way you can catch type-related errors in compile time.

The compiler itself doesn't care whether or not you used generics. it removes all of them on compilation and acts as if you didn't used generics. however, it won't let you compile if you have a compilation error in the first place.

I also noticed I didn't answered your question about the code.

When you do something like this

class LinkedList<T> {
....
.... 
}

You tell the compiler that this class supports generics, and in that case it is possible to do what I've said above

you could do

LinkedList<String> list = new LinkedList<String>();

Now, where ever in your class it says T it would acts as if it says String, thus allowing only adding and mainpulating Strings.

Upvotes: 6

kosa
kosa

Reputation: 66637

<> helps compiler in verifying type safety.

Compiler makes sure that List<MyObj> holds objects of type MyObj at compile time instead of runtime.

Generics are mainly for the purpose of type safety at compile time. All generic information will be replaced with concrete types after compilation due to type erasure.

Upvotes: 11

Code-Apprentice
Code-Apprentice

Reputation: 83527

<> can be used with any class you wish, not just containers. These are simply the most common since we want to be able to store any kind of Object we want in a container and still keep type safety. To understand this in more depth, you should research generics and what they are used for.

Upvotes: 1

Related Questions