简体   繁体   English

可以通过标识(即。==)比较Java Method对象,还是必须使用equals()?

[英]Can the Java Method object be compared by identity (ie. ==) or do I have to use equals()?

在Java中,可以通过标识(即。==)比较java.lang.reflect.Method对象的实例,还是必须使用equals()?

The question does not refer to the difference between == and equals in general, but refers to java.lang.reflect.Method instances in particular. 问题并不是指一般的==equals之间的区别,而是特别指的是java.lang.reflect.Method实例。

It is a reasonable question, because it could be justified to assume that there exists only one instance of each Method - similar to Class objects, which are created exactly once in the JVM. 这是一个合理的问题,因为可以证明每个Method只存在一个实例 - 类似于Class对象,它们在JVM中只创建一次

However, this is not the case: Two Method objects may be equal , even though they are not identical , as can be seen in this example (it also does the comparison for Class objects, to emphasize that these indeed are identical) 然而,这不是这种情况:两个Method对象可以是equal ,即使它们是不相同的 ,如在此示例中可以看出(它也做为比较Class对象,要强调的是,这些确实相同的)

package stackoverflow;

import java.lang.reflect.Method;

public class MethodEquals
{
    public static void main(String[] args) throws Exception
    {
        checkMethods();
        checkClasses();
    }

    static void checkMethods() throws Exception
    {
        Method m0 = MethodEquals.class.getMethod("exampleMethod", int.class);
        Method m1 = MethodEquals.class.getMethod("exampleMethod", int.class);

        boolean identical = (m0 == m1);
        boolean equal = m0.equals(m1);

        System.out.println("Methods: "+(identical == equal)); // prints "false"
    }

    static void checkClasses() throws Exception
    {
        Class<?> c0 = Class.forName("stackoverflow.MethodEquals");
        Class<?> c1 = Class.forName("stackoverflow.MethodEquals");

        boolean identical = (c0 == c1);
        boolean equal = c0.equals(c1);

        System.out.println("Classes: "+(identical == equal)); // prints "true"
    }

    public float exampleMethod(int i)
    {
        return 42.0f;
    }
}

暂无
暂无

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

相关问题 我如何在 Java 中使用字符的 equals() 方法 - how do I use the character's equals() method in Java 如何在Java中的equals覆盖方法中强制转换对象? - How can I cast an Object in an equals override method in Java? 在我的 contains 和 equals 方法中使用 Object 对象之前,是否必须将其强制转换为特定类型? - Do I have to cast an Object object to a specific type before using it in my contains and equals method? 如何使用布尔equals(object o)方法并调用应用该方法的对象? - How do I use the boolean equals(object o) method and call the object the method is applied to? 我无法从DialogFragment的EditText对象中获取文本(获取空字符串,即“”) - I can't grab the text from EditText object in DialogFragment ( getting an empty String ie. “” ) 我可以在Java中设置一个窗口,使其具有统一的大小(即正方形)吗? - Can I set a window in Java so it has uniform resizing (ie. a square)? 为什么我们必须覆盖 Java 中的 equals() 方法? - Why do we have to override the equals() method in Java? 我可以在Java中将对象用作方法的变量吗? - Can I use an object as a variable of a method in java? Java ArrayList 不使用重写的 equals(Object o) 方法进行比较 - Java ArrayList does not use overriden equals(Object o) method to compare 为什么在Java中使用默认的Object equals方法更好? - Why is it better to use the default Object equals method in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM