Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do you think class string implements interface comparable in Java

I'm doing this for my own understanding and I don't understand this question.

Assume there is an interface named comparable with the following definition:

public interface Comparable { 
        int compareTo(Object obj);
     }

Do you think class String implements interface Comparable? Provide a reason for your answer.

I would say no because there is no string anywhere in the class, but I believe I am misunderstanding the question. Can Someone explain this one better for me?

like image 239
beginningprogrammer Avatar asked Aug 10 '15 09:08

beginningprogrammer


People also ask

Does String class implement comparable?

String s implement the Comparable<String> interface. If an object is of a class that implements Comparable , then that object is less than, equal, or greater than any object of that class. compareTo() returns an integer to show which of these three relations hold.

How would you implement a comparable interface for a string in Java?

Using Comparable InterfaceOverride the compareTo method in the Pair class. Create an array of Pairs and populate the array. Use the Arrays. sort() function to sort the array.

Why would a class implement the comparable interface What is the method of the Comparable interface?

The Comparable interface defines the compareTo method used to compare objects. If a class implements the Comparable interface, objects created from that class can be sorted using Java's sorting algorithms.

When a class implements to comparable interface what method must also be implemented?

For any class to support sorting, it should implement the Comparable interface and override it's compareTo() method. It returns a negative integer if an object is less than the specified object, returns zero if an object is equal, and returns a positive integer if an object is greater than the specified object.


3 Answers

You should look into the source code or the Java doc of the class String. It is explicitly implementing the interface Comparable.

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html

java.lang

Class String

java.lang.Object

java.lang.String

All Implemented Interfaces:

Serializable, CharSequence, Comparable< String >

like image 105
dotvav Avatar answered Nov 13 '22 15:11

dotvav


I'm going to give you other point of view for the question and I'm goint to say No, it doesn't.

Since Java 6, java.lang.String can't implement that interface because that is not compatible with Comparable<String> and Comparable is not the same as Comparable<String>. The diference is that you can't compare an String with an Object

like image 20
JCalcines Avatar answered Nov 13 '22 16:11

JCalcines


String implements Comparable.

Why it does so?

So that sorting can be done on strings using compareTo method. For example you want to sort employees based on their name and name is string so you use this as:-

 import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class Employee implements Comparable {

    String name;

    public Employee(String name) {
        this.name = name;
    }

    public int compareTo(Object o) {
        Employee employeeAnother = (Employee) o;
        // natural alphabetical ordering by type
        // if equal returns 0, if greater returns +ve int,
        // if less returns -ve int
        return this.name.compareTo(employeeAnother.name);// String compareTo is
                                                            // called here
    }

    public static void main(String[] args) {
        List list = new ArrayList();
        list.add(new Employee("Ramesh"));
        list.add(new Employee("Deepak"));
        list.add(new Employee("Vivek"));
        Collections.sort(list); // sorts using compareTo method
        for (Iterator iter = list.iterator(); iter.hasNext();) {
            Employee employee = (Employee) iter.next();
            System.out.println(employee);
        }
    }

    public String toString() {
        return name;
    }
} 

I have not used Generics to keep things simple. You will get few warnings but you can ignore these.

like image 1
Goyal Vicky Avatar answered Nov 13 '22 15:11

Goyal Vicky