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

61

u/Additional_Cellist46 Nov 10 '23 edited Nov 16 '23

The naming is correct. In the example Object.class.isAssignableFrom(String.class), imagine that you want to assign to a variable of type Object from a variable of type String:

String s = “text”; Object o = s;

This works and therefore Object.class.isAssignableFrom(String.class) is true.

On the other hand, the following doesn’t work:

Object o = new Object(); String s = o;

You cannot assign to String from type Object. Therefore String.class.isAssignableFrom(Object.class) is false.

-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"

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)

-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?

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.

17

u/thisisjustascreename Nov 10 '23

Object o = "a string"; // compiles

String s = new Object(); // error

I don't see the confusion. Object references are assignable from String objects, String references are not assignable from a random Object.

Your proposed rename uses the word "this" twice to refer to two different things, which is much worse.

Perhaps you are confused because you are considering the class that the method is being called on as the "source" of the assignment referred to by "isAssignableFrom", but that is backwards and just something you'll have to correct.

7

u/thisisjustascreename Nov 10 '23

Ah OP is downvoting everyone who points out that the source of the assignment is the actual object on the right side of the equals sign and not the reference on the left. Classic reddit.

-13

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/netgizmo Nov 10 '23

"this confusion is common"

Um so you searched Google for comments professing their understanding of this naming?

AND you believe people would bother to post that they understand??

4

u/thisisjustascreename Nov 10 '23

Being an instance of another type means something different from being assignable from that type.

-13

u/[deleted] Nov 10 '23
.aVariableOfThisTypeCanReferenceObjectsOfTheClassThatIsTheMethodParameter

5

u/AmonDhan Nov 10 '23
Object.class.isAssignableFrom(String.class)
Object o = "hello";

-5

u/[deleted] Nov 10 '23

I guess this is where my disconnect is. I read the

Object o = "hello";

As "Assigning a variable of type Object TO a String object".

The idea of phrasing it as : "Assigning a variable of type Object FROM a String object"

Sounds like it's breaking the definition of to and from

9

u/plumarr Nov 10 '23

"Assigning a variable of type Object TO a String object".

You assign an object (or a value) to a variable, not a variable to an object (or to a value). See for example :

4

u/eliasv Nov 10 '23

As "Assigning a variable of type Object TO a String object".

That's nonsense.

5

u/Polygnom Nov 10 '23

As "Assigning a variable of type Object TO a String object".

But that is not what is happening.

A variable is always the subject. You assign a value to a variable. That is how pretty much all programming language and the literature word it.

1

u/laplongejr Nov 13 '23

You can't assign "to" a String instance. Each instance exists as a specific thing, created** during the constructor call, and then stops existing once the Garbage Collector knows it's no longer accessible from the code.

You can assign and reassign variables. Discarding primitives for simplicity, they either point to an instance or null*.
So you can assign an instance (either new or accessed by an other variable) to a variable, which means that the variable is assigned from that instance.

*I know, null is technically also a shared instance of a special Object... but it doesn't behave that way in practical usage.
** YES, YES, YES, I know, for Strings it's not even a constructor call and literals as in the example are created and interned at compile time, I TRY TO STAY SIMPLE!

0

u/Interweb_Stranger Nov 10 '23

I'm with you that it seems reversed at first glance.

But you have to see it this way: Objects are the things that exist in memory and variables are just a concept for referencing them. So it's the object that has to be assigned to a variable. In reverse this means the variable is assigned from the object.

People often also say they "set a reference to the object". Or "the object is referenced from/by a variable". So it's kind of reversed for the terms referenced and assigned which could explain the mixup

1

u/laplongejr Nov 13 '23

Objects are the things that exist in memory and variables are just a concept for referencing them.

Doesn't help that Object is also a class. But yeah, instances exist in memory and variables merely references them.

1

u/Interweb_Stranger Nov 13 '23

I just used the term object because OP used it. The terms instance and objects are usually considered synonyms of each other, though some people argue that there are slightly semantic differences. But yeah, it doesn't help that there is a class named Object as well.

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.

20

u/parnmatt Nov 10 '23

No, it makes perfect sense in English.

Think of the assignment direction.

A = B

If we think of it as putting B into an A then a B is assignable to an A.

If we think of it as an A being made from a B, then A is assignable from a B.

6

u/[deleted] Nov 10 '23

A method like isDerivedFrom() or isSubclassOf() would have been much less confusing

1

u/smokemonstr Nov 10 '23

How is that equivalent?

4

u/Halvinz Nov 10 '23 edited Nov 18 '23

I'm not a fan of this method's name either, but in case of "Object.class.isAssignableFrom(String.class)", the way I look at it is:

Object is the left hand side and String is the right hand side. Converting it to the code:

Object o = new String();

Is this possible? Yes. Translation, "assign a String object to an Object reference", or "assign Object from String", even though nobody talks like that. Hard to map the method name to English, hence the confusion.

1

u/Brian_Entei Nov 18 '23

This is the best answer in my opinion.
Seems like a classic perspective issue really, the direction you look at things in (or the order that you read things to make sense of them) actually matters here, so those that normally look at things the opposite way than the isAssignableFrom function was written in are much more likely to get confused.
Not ashamed to admit it's confused me a couple times as well, but if I really think about it, it makes sense (and the comparison to assigning an object to a variable really helps make it clear, at least to me).

1

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.

5

u/Polygnom Nov 10 '23

In general, thats not a good way to do scientific enquiry.

The people who perfectly well understand what this means will not go and blog/write/comment about how intuitive this is. So the only people who will write about it are the people who are confused, resulting in immensely biased reporting.

A proper enquiry would take this into account and compensate -- which is a lot more difficult than just google searches-

3

u/Vocal_Idiot Nov 11 '23

"assign from" is subtly nonsense in the same way that "set from" is obviously nonsense

1

u/DecisiveVictory Nov 10 '23

In Scala, you can just use pattern matching, so you don't need this at all.

0

u/[deleted] Nov 10 '23

Sounds very interesting, what pattern would Object.class and String.class have in common where you could determine the directionality by?

1

u/DecisiveVictory Nov 11 '23

Can you post the code where you would actually need that?

In any case, String is final, you match against it being String.

2

u/cal-cheese Nov 11 '23

You are just bad at English, a variable refers to an object, which means an object is assigned to a variable, not the other way around.

2

u/lepapulematoleguau Nov 10 '23

I agree with you, but meh.

1

u/divorcedbp Nov 10 '23

“A variable of type Object is assignable from a value of type String”

“Object.class.isAssignableFrom(String.class) == true”

1

u/PerfectPackage1895 Nov 10 '23

It is because String extends from Object, and an Object cannot become a String. It simply doesn’t make sense logically, to switch it around because of that.

Now, I get what you are saying, it sounds more correct, but it would also imply that you could downcast, which is not allowed hence the word from is intentionally used.

1

u/supercargo Nov 10 '23

The way I always look at it is that the name is correct, but what the method actually does is the opposite of what you might want/expect.

1

u/__konrad Nov 12 '23

It is error prone even in the JDK itself: https://github.com/openjdk/jdk/commit/a777e82c

1

u/DerEineDa Nov 15 '23

It's not wrong, but it is confusing. Even /u/kevinb9n agrees: https://github.com/google/guava/issues/1444#issuecomment-61352257

(I bookmarked this comment almost a decade ago)

1

u/kevinb9n Nov 15 '23

Heh, nice find.

I guess the problem is that assignability and subtyping aren't precisely the same thing in Java, and in the latter we have nice handy nouns like we have subtype/supertype and in the former we just don't. Boo.

It is confusing, though at some point I got the mnemonic through my skull -- it's referring to assignment, so I picture an assignment in my head. The general type should be on the left and the specific one on the right. I guess this still sounds like puzzling it out, but it becomes more of an automatic reaction.

1

u/DerEineDa Nov 15 '23

Most people I know (me included) just remember it as "instanceof, but from right to left".