4

When I call Set.class.isAssignableFrom(Iterable.class), it returns false.

Nevertheless, in the docs, java.util.Set is listed as a subinterface of java.lang.Iterable. Hence my confusion. You can even try it out in a single line of code:

System.out.println(Set.class.getName() + " is " + ((Set.class.isAssignableFrom(Iterable.class)) ? "" : "NOT " ) + "assignable from " + Iterable.class.getName());

it prints java.util.Set is NOT assignable from java.lang.Iterable.

Why is that?

1 Answer 1

5

That's because you're using isAssignableFrom wrong.

As the docs say, isAssignableFrom(Class<?> cls) "determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter". So cls would be Set.class, and the full syntax would be:

Iterable.class.isAssignableFrom(Set.class).

...which, indeed, returns true.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.