Question: Consider the following implementation of the compareTo() method for String . How does the second line help with efficiency? public int compareTo(String that) { if

Consider the following implementation of the compareTo() method for String. How does the second line help with efficiency?

public int compareTo(String that) { if (this == that) return 0; // this line int n = Math.min(this.length(), that.length()); for (int i = 0; i < n; i++) {

if (this.charAt(i) < that.charAt(i)) return -1; else if (this.charAt(i) > that.charAt(i)) return +1;

}

return this.length() - that.length();

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!