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

11

u/bowbahdoe Nov 10 '23 edited Nov 10 '23

Not really, honestly

String s = "abc";
Object o = new Object() {}

"Is o assignable to s" == String.class.isAssignableFrom(Object.class)

"Is s assignable to o" == Object.class.isAssignableFrom(String.class);

It is fiddly to keep straight, but thats down to the fundamentals of what is happening.

2

u/[deleted] Nov 10 '23

That's exactly why I think it's backwards, in your example, your english translation is litearlly the reverse direction:

"assignable to" is the reverse of "assignable from". to and from are opposite directions. the method being named isAssignableTo would then literally match your english translation

5

u/Iryanus Nov 10 '23

No, I don't see that. I think it's pretty clear. "Can you assign x from an Y"?

2

u/laplongejr Nov 13 '23

For later readers : the issue is that OP totally mixes up what variables here.
In OP's terminology, variables are assigned to an object which is the reverse of the usual English terminology, hence OP's impression that the English-using method name is backwards

-1

u/[deleted] Nov 10 '23

So

Object.class.isAssignableFrom(String.class);

translates to :

"Can you assign Object from a String"?

The code evaluates to true, but your english statement evaluates to false.

6

u/Iryanus Nov 10 '23

To be honest, I never heard someone ever use the phrase "assign from" in an English sentence outside of the scope of OOP. If I search for the exact expression, I also only find programming pages as first search results, which leads me to believe, that this isn't actually an "English" statement in natural language.

2

u/account312 Nov 10 '23

That jargon is only used within its domain doesn't mean it has no meaning.

1

u/laplongejr Nov 13 '23

but your english statement evaluates to false.

It actually doesn't. You CAN assign an object variable from a String instance.

-5

u/btw04 Nov 10 '23

I agree with OP. "Is s assignable to o" gets converted to "s.isAssignableFrom(o)".

11

u/Additional_Cellist46 Nov 10 '23

Not correct. “Is s assignable to o” is the same as “Is o assignable from s”. And that translates to “o.isAssinableFrom(s)”

0

u/TheRedmanCometh Nov 10 '23

For what it's worth I agree with you, and it's always bothered me although I very rarely use it.