1

While writing code to check the datatype of entries in a Java 11 array, I came across a compile-time error which I can't seem to find anywhere on Google or SO. I'm using JetBrains's IntelliJ IDEA, and the editor is giving me the error "Inconvertible types; cannot cast 'double' to 'double'." for the second line in the following MWE (that is, put inside a main method etc.):

double[] arr = new double[] {0.1, 0.2};
boolean result = arr[0] instanceof double;

I don't really get what's wrong here. Isn't instanceof exactly analogous to Python's isinstance? New to Java (not to Python nor the Java derivative Processing), so this baffles me.

Mew
  • 368
  • 1
  • 11
  • 1
    If I recall correctly, `instanceof` does not work with primitive data types. Only objects. – Tim Hunter Feb 28 '20 at 21:53
  • What would be the point? Primitives don't support inheritance. – shmosel Feb 28 '20 at 21:54
  • @azurefrog: Turning the second line into `boolean result = Double.class.isInstance(arr[0]);` seems to work! Also, if you don't mind me following up: isn't my IDE incorrect in saying a `double` can't be cast to a `double`? I recall that that would show up greyed-out in IntelliJ (since it'd be redundant), but certainly not impossible ... – Mew Feb 28 '20 at 22:06

0 Answers0