# Why doesn't 'is' type check work on Java classes

**URL:** <https://discuss.kotlinlang.org/t/why-doesnt-is-type-check-work-on-java-classes/4660>\
**Category:** Uncategorized\
**Created:** [September 20, 2017, 1:41pm UTC](https://discuss.kotlinlang.org/t/why-doesnt-is-type-check-work-on-java-classes/4660 "2017-09-20T13:41:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stas.Shusha](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/stas.shusha/32/3082_2.png) [@Stas.Shusha](https://discuss.kotlinlang.org/u/Stas.Shusha)\
**Post date:** [September 20, 2017, 1:41pm UTC](https://discuss.kotlinlang.org/t/why-doesnt-is-type-check-work-on-java-classes/4660/1 "2017-09-20T13:41:35Z")

</div>

Hi, could someone explain me why does “is” typecheck fail in such a simple scenario when checking Java class from Kotlin code:

```
//JavaClass.java
public class JavaClass {}

//Kotlin.kt
open class Base
class Derived : Base()
class SomeOtherKotlinClass

fun test1(base : Base) = base is Derived //compiles, as expected
fun test2(base : Base) = base is SomeOtherKotlinClass //fails, as expected
fun test3(base : Base) = base is JavaClass //compiles, but should also fail as JavaClass does not derive from Base

```

---

<div class="post-metadata">

**Author:** ![CNife](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/cnife/32/2746_2.png) [@CNife](https://discuss.kotlinlang.org/u/CNife)\
**Post date:** [September 20, 2017, 4:07pm UTC](https://discuss.kotlinlang.org/t/why-doesnt-is-type-check-work-on-java-classes/4660/2 "2017-09-20T16:07:04Z")

</div>

I don’t know how you test code written.  
My test code:

```nohighlight
fun main(args: Array<String>) {
    println(test3(Base())) // false
    // println(test2(Base())) Can't compile test2 func
    println(test1(Base())) // false
}

```

Maybe I misunderstood your “ **fail** ”.  
If you meant `test2` couldn’t be compiled while `test3` could, it’s may because Kotlin can’t apply type checking stricty to Java types.

---

<div class="post-metadata">

**Author:** ![Stas.Shusha](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/stas.shusha/32/3082_2.png) [@Stas.Shusha](https://discuss.kotlinlang.org/u/Stas.Shusha)\
**Post date:** [September 20, 2017, 4:37pm UTC](https://discuss.kotlinlang.org/t/why-doesnt-is-type-check-work-on-java-classes/4660/3 "2017-09-20T16:37:33Z")

</div>

yeah, runtime check works, I’m talking about compile time check. I wonder why does it not work and are there any plans to implement it?

---

<div class="post-metadata">

**Author:** ![CNife](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/cnife/32/2746_2.png) [@CNife](https://discuss.kotlinlang.org/u/CNife)\
**Post date:** [September 21, 2017, 6:40am UTC](https://discuss.kotlinlang.org/t/why-doesnt-is-type-check-work-on-java-classes/4660/4 "2017-09-21T06:40:27Z")

</div>

`is` operator in Kotlin will be simply compiled to the same bytecode as `instanceof` operator in Java.  
For example, the function below

```nohighlight
fun testIs(any: Any?) = any is String

```

will be compiled to

```auto
aload 0
instanceof java/lang/String
ireturn

```

**Both Kotlin’s `is` and Java’s `instanceof` operator will give your a compile error when you are trying to apply an instance to a incompatible type.**

```nohighlight
//Java Test 
System.out.println(new StringBuilder() instanceof String); // compile error

```

```nohighlight
//Kotlin Test
println(StringBuilder() is String) // compile error

```

**Compile errors only disappear when you are applying an instance to a Java type which hasn’t mapped to a Kotlin type.**  
The Kotlin test code above works because `kotlin.String` is a Kotlin type. You could find all the mapped types here: [Mapped Types](https://kotlinlang.org/docs/reference/java-interop.html#mapped-types)  
Also something about [Platform Types](https://kotlinlang.org/docs/reference/java-interop.html#null-safety-and-platform-types)
