Can someone help me understand why this returns false
?
'z'.getClass.isInstance('z')
Can someone help me understand why this returns false
?
'z'.getClass.isInstance('z')
'z'.getClass
returns the class for the primitive char
type on JVM, but isInstance
expects a value which is subtype of java.lang.Object
(so z
will be boxed to java.lang.Character
). In this case a call to isInstance
on a java.lang.Class
for a primitive type will never return true.
ah ok, the following returns true
classOf[java.lang.Character].isInstance('z')
Your answer was helpful. However, for clarity could you pleas edit the answer and clarify what you mean by a class returning a value. Classes do not return values. Do you mean the isInstance method of a class returning a value, or do you mean the method returning a value when given an instance of a class.
Thanks for the feedback!, I have edited my answer