JonLunde
JonLunde

Reputation: 151

Comparable<T> and compareTo

I don't understand how interfaces like Comparable works exactly, does it include a written compareTo-method that I can use? Or do I have to @override and create one myself?

Upvotes: 0

Views: 201

Answers (1)

wake-0
wake-0

Reputation: 3966

Compareable<T> contains a compareTo method with the type parameter T. See the documentation: link

You also get the explicit type parameter from the object, so you don't need any cast. Some datatypes already have implemented compareTo e.g.

Integer x = 5;
int compareValue = x.compareTo(3);

String str = "Hallo";
compareValue = str.compareTo("Hallo");

Upvotes: 1

Related Questions