mardi 13 octobre 2020

How does Class::getMethod with an Object.class parameter work?

public static void main(String[] args) throws NoSuchMethodException, SecurityException {
    Method method = String.class.getMethod("compareTo", Object.class);
    System.out.println(method);
}

Prints public int java.lang.String.compareTo(java.lang.Object).

public static void main(String[] args) throws NoSuchMethodException, SecurityException {
    "Foo".compareTo(new Object());
}

Doesn't compile, because The method compareTo(String) in the type String is not applicable for the arguments (Object).

Why?





Aucun commentaire:

Enregistrer un commentaire