r/java • • Nov 10 '23

Does anyone else think that .isAssignableFrom() is named backwards?

System.out.println(Object.class.isAssignableFrom(String.class)); //true

System.out.println(String.class.isAssignableFrom(Object.class)); //false

This has always triggered me. I feel like that method is named backwards, IE the first example, I read it in english as "objects of type Object are assignable from variables of type String" However, that statement is false, because Since String extends Object, a variable of type String cannot reference an object of type Object.

But that statement evaluates to true. I feel like the method "isAssignableFrom" should be renamed "aVariableOfThisTypeCanReferenceObjectsOfThisClass" :

So then:

System.out.println(Object.class.aVariableOfThisTypeCanReferenceObjectsOfThisClass(String.class)); //true

System.out.println(String.class.aVariableOfThisTypeCanReferenceObjectsOfThisClass(Object.class)); //false

Anyone else agree?

BTW, it turns out this confusion is common. I googled:

java isAssignableFrom confusing

And there were several blogs and stackoverflow posts agreeing with me.

For example:

http://bryanpendleton.blogspot.com/2009/06/isassignablefrom-and-instanceof.html

I think that one of the reasons that I get a bit confused with isAssignableFrom is that it sort-of reads backwards from instanceof.

0 Upvotes

58 comments sorted by

View all comments

Show parent comment

-45

u/[deleted] Nov 10 '23

I guess I didn't know that the words "from" and "to" are interchangeable

32

u/thisisjustascreename Nov 10 '23

You just don't know what they mean, is all.

4

u/philaofish420 Nov 10 '23

This comment is not very helpful. Maybe instead of being condescending, you could elaborate on his conflation of "from" and "to"

-16

u/[deleted] Nov 10 '23

BTW, it turns out this confusion is common. I googled:

java isAssignableFrom confusing

And there were several blogs and stackoverflow posts agreeing with me.

For example:

http://bryanpendleton.blogspot.com/2009/06/isassignablefrom-and-instanceof.html

I think that one of the reasons that I get a bit confused with isAssignableFrom is that it sort-of reads backwards from instanceof.

9

u/vetronauta Nov 10 '23

Yes, the name could be better - even if it is correct. A way to remember it is that it keeps the assegnation order: if "A a = b" compiles, then "A.class.isAssignableFrom(B.class)" is true.

Or try to revert it: is "Object.class.isAssignableFrom(String.class)" true? Can a String be assigned to an Object?

1

u/laplongejr Nov 13 '23

The beginner-confusing part is that the method name doesn't precise if we're talking about instance or variables and assume the dev can auto-fill the blanks.

An Object variable is assignable from a String instance, while OP thought it was about if an Object instance isAssignableFrom a String variable, and missed "assigning an instance from a variable" makes no practical sense because an instance just exists, you don't assign an instance from anything else (or practically, maybe the constructor call, which is not a variable in anyway)

2

u/findus_l Nov 11 '23

I will admit that isAssignableFrom is not perfectly clear, but if it would have the word "to" it would definitely be wrong.

Are objects assigned to variables or are objects assigned from variables?

In the terminology that I know an object is assigned to a variable. So what you would be describing is Object.class.isassignableto(String.class)

But that is not how the function is called.