Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do < and > mean such as implements Comparable<BigInteger>?

In Java 1.4.2, class java.math.BigInteger implements interfaces Comparable, Serializable.

In Java 1.5.0, class java.math.BigInteger implements interfaces Serializable, Comparable<BigInteger>.

This is just an example to help me ask about < and >. What I am really wondering about is the < and > stuff.

My question is threefold:

  • what does the <BigInteger> part of the implements statement mean?
  • what is that syntax called?
  • and what does it do?

P.S.: It's really hard to google for < and > and impossible to search SO for < and > in the first place.

Thanks!

like image 667
eleven81 Avatar asked Jan 16 '09 14:01

eleven81


People also ask

What is implements comparable in Java?

The Comparable interface defines the compareTo method used to compare objects. If a class implements the Comparable interface, objects created from that class can be sorted using Java's sorting algorithms.

Why do we implement comparable in Java?

The Comparable interface is used to compare an object of the same class with an instance of that class, it provides ordering of data for objects of the user-defined class.

What are the benefits of implementing the Comparable interface?

The benefit of implementing the interface is that some methods specifically require object that implements the Comparable interface. It gives them a guarantee that the object you're passing has a compareTo method with the correct signature.

When a class implements to comparable interface what method must also be implemented?

For any class to support sorting, it should implement the Comparable interface and override it's compareTo() method. It returns a negative integer if an object is less than the specified object, returns zero if an object is equal, and returns a positive integer if an object is greater than the specified object.


2 Answers

Read the Java Generics Tutorial. The thing between the angle brackets is a type parameter - Comparable is a generic class, and in this case the angle brackets mean that the class is comparable to other BigIntegers.

For a little more clarification in this case, have a look at the Javadocs for Comparable in 1.5. Note that it is declared as Comparable<T>, and that the compareTo method takes an argument of type T. The T is a type parameter that is "filled in" when the interface is used. Thus in this case, declaring you implement Comparable<BigInteger> implies that you must have a compareTo(BigInteger o) method. Another class might implement Comparable<String> meaning that it would have to implement a compareTo(String o) method.

Hopefully you can see the benefit from the above snippet. In 1.4, the signature of compareTo could only ever take an Object since all kinds of classes implemented Comparable and there was no way to know exactly what was needed. With generics, however, you can specify that you are comparable with respect to a particular class, and then write a more specific compareTo method that only takes that class as a parameter.

The benefits here are two-fold. Firstly, you don't need to do an instanceof check and a cast in your method's implementation. Secondly, the compiler can do a lot more type checking at compile time - you can't accidentally pass a String into something that implements Comparable<BigInteger>, since the types don't match. It's much better for the compiler to be able to point this out to you, rather than have this cause a runtime exception as would have generally happened in non-generic code.

like image 102
Andrzej Doyle Avatar answered Nov 14 '22 22:11

Andrzej Doyle


I'm pretty sure it is Generics

http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html

I asked something similar (C#) it has useful info there What does Method<ClassName> mean?

like image 23
Ian G Avatar answered Nov 14 '22 21:11

Ian G