简体   繁体   English

你认为类字符串实现了Java中可比的接口

[英]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?你认为 String 类实现了接口 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?有人可以为我更好地解释这个吗?

You should look into the source code or the Java doc of the class String .您应该查看String类的源代码或 Java 文档。 It is explicitly implementing the interface Comparable .它明确地实现了接口Comparable

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html 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 >可序列化、CharSequence、 Comparable<String>

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> .从 Java 6 开始, java.lang.String 无法实现该接口,因为它与Comparable<String>不兼容,并且ComparableComparable<String> The diference is that you can't compare an String with an Object不同之处在于您不能StringObject进行比较

String implements Comparable.字符串实现了 Comparable。

Why it does so?

So that sorting can be done on strings using compareTo method.这样可以使用 compareTo 方法对字符串进行排序。 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.您将收到很少的警告,但您可以忽略这些警告。

If you want to check if the given class implements some interfaces or other class you could use instanceof :如果你想检查给定的类是否实现了一些接口或其他类,你可以使用instanceof

System.out.println("string" instanceof Comparable);

You could also try to execute method from the given interface:您也可以尝试从给定的接口执行方法:

"one".compareTo("two")

In this case your code will compile or appropriate error message will be shown.在这种情况下,您的代码将被编译或显示适当的错误消息。


However the best way is to read the source code of the given class.然而,最好的方法是阅读给定类的源代码。 In this case you don't have to write any additional code.在这种情况下,您不必编写任何额外的代码。 Also, a good IDE (like Eclipse, Intellij Idea) could help to find the answer.此外,一个好的 IDE(如 Eclipse、Intellij Idea)可以帮助找到答案。

yes!!是的!! In Java 8;在 Java 8 中; open Java doc and you will find below:打开 Java 文档,您将在下面找到:

public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM