CHAPTER 53B — Comparable Interface

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.

created: 6/26/2006

CHAPTER 53B — Comparable Interface

A class implements the Comparable interface when its objects are expected to be arranged into a particular order. For example, the class String implements Comparable, because strings are often compared with each other and arranged into alphabetical order. Numeric classes (such as Integer or Double) implement Comparable since number are often sorted into numeric order.

Chapter Topics:

  • The Comparable Interface
  • comparable() method
  • natural order
  • collating sequence
  • sorting
  • binary search

This chapter describes the Java 25 LTS version of Comparable, which uses generics. Earlier versions of Java are somewhat different.

QUESTION 1:

Arrange these strings into order: "orange", "apple", "plum".