# Check class type of two vars

**URL:** <https://discuss.kotlinlang.org/t/check-class-type-of-two-vars/13182>\
**Category:** Support\
**Created:** [June 22, 2019, 5:02pm UTC](https://discuss.kotlinlang.org/t/check-class-type-of-two-vars/13182 "2019-06-22T17:02:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![giovi.andre](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@giovi.andre](https://discuss.kotlinlang.org/u/giovi.andre)\
**Post date:** [June 22, 2019, 5:02pm UTC](https://discuss.kotlinlang.org/t/check-class-type-of-two-vars/13182/1 "2019-06-22T17:02:27Z")

</div>

Hi everyone.

I would like to know if there is a way to do something like this:

var a: Any → get the value somewhere  
var b: String = “Hello”

a::class.java is b::class.java

Actually, the only thing I can do is:

a::class.java is String

Thanks, Giovanni

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [June 22, 2019, 5:25pm UTC](https://discuss.kotlinlang.org/t/check-class-type-of-two-vars/13182/2 "2019-06-22T17:25:21Z")

</div>

`a::class == b::class`

---

<div class="post-metadata">

**Author:** ![thorwing](https://avatars.discourse-cdn.com/v4/letter/t/49beb7/32.png) [@thorwing](https://discuss.kotlinlang.org/u/thorwing)\
**Post date:** [June 22, 2019, 5:28pm UTC](https://discuss.kotlinlang.org/t/check-class-type-of-two-vars/13182/3 "2019-06-22T17:28:06Z")

</div>

@darksnake  
That still won’t work at compile time.

Normally, you wouldn’t be able to do that because you already know both types so you always know it’s not true. However, a dynamic approach would be to simply do

`a::class.java.isInstance(b::class.java)`

---

<div class="post-metadata">

**Author:** ![giovi.andre](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@giovi.andre](https://discuss.kotlinlang.org/u/giovi.andre)\
**Post date:** [June 22, 2019, 5:30pm UTC](https://discuss.kotlinlang.org/t/check-class-type-of-two-vars/13182/4 "2019-06-22T17:30:02Z")

</div>

Sorry. the example I did wasn’t so much correct.

I’ll provide to correct it now

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [June 22, 2019, 5:32pm UTC](https://discuss.kotlinlang.org/t/check-class-type-of-two-vars/13182/5 "2019-06-22T17:32:53Z")

</div>

It will give IDE warning. As for your sample, it checks subtyping, not equality. Also you can do it with KClass, without .java call.

---

<div class="post-metadata">

**Author:** ![thorwing](https://avatars.discourse-cdn.com/v4/letter/t/49beb7/32.png) [@thorwing](https://discuss.kotlinlang.org/u/thorwing)\
**Post date:** [June 22, 2019, 5:34pm UTC](https://discuss.kotlinlang.org/t/check-class-type-of-two-vars/13182/6 "2019-06-22T17:34:53Z")

</div>

`a::class == b::class`

is not the same check as

`a is b`

one is equality, the other is conformity

---

<div class="post-metadata">

**Author:** ![giovi.andre](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@giovi.andre](https://discuss.kotlinlang.org/u/giovi.andre)\
**Post date:** [June 22, 2019, 5:38pm UTC](https://discuss.kotlinlang.org/t/check-class-type-of-two-vars/13182/7 "2019-06-22T17:38:09Z")

</div>

I tried all your response but none of them works.

I think the main problem is thath I’m checking the class type of an Any object with the class type of a var

---

<div class="post-metadata">

**Author:** ![giovi.andre](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@giovi.andre](https://discuss.kotlinlang.org/u/giovi.andre)\
**Post date:** [June 22, 2019, 5:59pm UTC](https://discuss.kotlinlang.org/t/check-class-type-of-two-vars/13182/8 "2019-06-22T17:59:23Z")

</div>

Ok, I’ve got it.

Property to cast an Any obj to T obj:

fun Any.castTo(c: Class): T {  
return this as T  
}

Example:

a : Any  
b : String

and then I check if a is b in this way :

if ( a.toObj(b::class.java)::class == b::class) {  
//Stuff with a as String  
}

Hope it will be usefull to other people
