Java Comparable Interface Last Updated : 04 Oct, 2025 Comments Improve Suggest changes 25 Likes Like Report The Comparable interface in Java is used to define the natural ordering of objects. It allows objects of a class to be compared to each other, which is essential for sorting and ordering collections like arrays, lists, or sets. The interface is part of the java.lang package.Interface Declaration Java public interface Comparable<T> { int compareTo(T obj); } The Comparable interface imposes a total ordering on the objects of the implementing class.Classes that implement Comparable must override the compareTo() method.The compareTo() method defines the natural ordering and is used by methods like Collections.sort() or Arrays.sort().Methods1. int compareTo(T obj)Compares the current object with the specified object obj. Returns a negative integer, zero, or a positive integer if the current object is less than, equal to, or greater than the specified object.Return Value of compareTo()Negative Value (< 0): Current object is less than the specified object.Zero (0): Current object is equal to the specified object.Positive Value (> 0): Current object is greater than the specified object.Implementing Comparable Java import java.util.*; class Student implements Comparable<Student> { String name; int marks; Student(String name, int marks) { this.name = name; this.marks = marks; } @Override public int compareTo(Student other) { return this.marks - other.marks; // ascending order by marks } @Override public String toString() { return name + ": " + marks; } } public class ComparableExample { public static void main(String[] args) { List<Student> students = new ArrayList<>(); students.add(new Student("Alice", 85)); students.add(new Student("Bob", 92)); students.add(new Student("Charlie", 78)); Collections.sort(students); for (Student s : students) { System.out.println(s); } } } OutputCharlie: 78 Alice: 85 Bob: 92 Natural Ordering: The compareTo() method defines how objects are naturally ordered.Type Safety: Comparable is generic (Comparable<T>), ensuring type safety.Sorting Collections: Collections and arrays can be sorted using compareTo() without explicitly providing a comparator.Primitive-like Ordering: Numeric types, strings, and dates in Java implement Comparable by default.Sorting Strings Java import java.util.*; public class StringSortExample { public static void main(String[] args) { String[] names = {"Alice", "Bob", "Charlie"}; Arrays.sort(names); // uses natural ordering (alphabetical) System.out.println(Arrays.toString(names)); } } Output[Alice, Bob, Charlie] AdvantagesSimplifies sorting of objects in lists or arrays.Works seamlessly with collection utilities like Collections.sort() and Arrays.sort().Provides type-safe comparisons when using generics. Comparable interface Visit Course Comparable interface Comparable Interface in Java with Examples Comment A ashutosh44 Follow 25 Improve A ashutosh44 Follow 25 Improve Article Tags : Java Geeks Premier League Geeks-Premier-League-2022 java-interfaces Java-Comparable Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings8 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java9 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like