Help Explain Java Function

Can someone please do two things for me.

import java.util.Arrays;

class Person implements Comparable<Person> {
  public Person(String firstName, String surname) {
    this.firstName = firstName;
    this.surname = surname;
  public String toString() {
    return firstName + " " + surname;
  public int compareTo(Person person) {
    int result = surname.compareTo(person.surname);
    return result == 0 ? firstName.compareTo(((Person) person).firstName) : result;
  private String firstName;
  private String surname;
public class MainClass {
  public static void main(String[] args) {
    Person[] authors = { new Person("A", "B"), 
                         new Person("C", "D"),
                         new Person("E", "F"), 
                         new Person("Z", "Y"),
                         new Person("X", "T"), 
                         new Person("O", "R") };
    System.out.println("\nThe cast is ascending sequence is:\n");
    for (Person person : authors) {

Open in new window

1) Tell me when does the compareTo function get called? Because Comparable is implemented, does it automatically get ran when the Person class is initiated?

2) Rewrite the line:

return result == 0 ? firstName.compareTo(((Person) person).firstName) : result;

to a more simplified if statement

Who is Participating?
mlempertConnect With a Mentor Commented:
1) Arrays.sort(authors); calls compareTo() on the Objects in the array.  compareTo()  is not called when the the Object is instantiated.

if (result ==0) {
    result = firstName.compareTo(person.firstName);
return result;
InquisitiveProgrammerAuthor Commented:
How does the program know not to use the generic Arrays.sort method?
What do you mean by generic Arrays.sort method ?

I think your invocation

corresponds only to this method
public static void sort(Object[] a)[]%29

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

InquisitiveProgrammerAuthor Commented:
What I mean is, how does the line


know to call the compareTo function of the Person class
Well, it looks at your array - it figures out that your array is composed of objects of the type Person
and uses Person's compareTo function.
that is something which is called polymorphism in Java -
when a method is invoked on the object, then the method corresponding to
the actual type of the instance is invoked (basically method corresponding to new command
which was used to construct this instance) is invoked, not the method of its parent.

It is like this:

class A {

public void method(){
//some stuff


class B extends A {

public void method() {
// another stuff


class TestPolymorphism {

public static void main(String [] args){

A a = new A();

A b = new B();


// at this point if you invoke method b.method() then   //another stuff   will be exected from overriden method of subclass
// not the method of superclass, even though you declared b as belonging to type A
// the method corresponding to creation of b with new B() operator will work
//this is called polymorphism; it applies only to methods; variables of inherited classes behave differently



Therefore even though sort method expects Objects it will recognize that your objects are in fact Persons
at the time when it needs to invoke their

read more about polymorphism here:

All elements in the array must implement the Comparable interface which forces your objects to have the compareTo(Object) method implemented.
The sort will cast each element of the array as a Comparable and call compareTo(Object) to order the array.  This is a snippet from the Arrays class:
            for (int i=low; i<high; i++)
                for (int j=i; j>low &&
                   ((Comparable)dest[j-1]).compareTo((Comparable)dest[j]) > 0; j--)
                      swap(dest, j, j-1);
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.