# Equality not working as expected with Kotlin code testing Java class?

**URL:** <https://discuss.kotlinlang.org/t/equality-not-working-as-expected-with-kotlin-code-testing-java-class/1820>\
**Category:** Support\
**Created:** [June 24, 2016, 10:17pm UTC](https://discuss.kotlinlang.org/t/equality-not-working-as-expected-with-kotlin-code-testing-java-class/1820 "2016-06-24T22:17:47Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![kotlinnewb](https://avatars.discourse-cdn.com/v4/letter/k/48db29/32.png) [@kotlinnewb](https://discuss.kotlinlang.org/u/kotlinnewb)\
**Post date:** [June 24, 2016, 10:17pm UTC](https://discuss.kotlinlang.org/t/equality-not-working-as-expected-with-kotlin-code-testing-java-class/1820/1 "2016-06-24T22:17:47Z")

</div>

Hi all, I converted several test classes to Kotlin and now have a test failing.

This is a pared down version of the class being tested. The issue is when kotlin tests this class it is unable to find the item in the `find` method whereas the Java variant is.

```
public class ObjectTracker {
    private final List<Object> mList = new ArrayList<>();

    public void add(Object object) {
        mList.add(object);
    }

    public Object find(Object value) {
        for (Object listItem : mList) {
            if (listItem == value) {
                return listItem;
            }
        }

        return null;
    }
}

```

Below is the original code and the code that resulted from the “Convert to Kotlin” action. Both test cases pass with the original Java code. With the Kotlin code only the “test string” test passes:

```
@Test
public void testAddingAndFindingInteger() {
    ObjectTracker objectTracker = new ObjectTracker();
    Integer value = 1000;
    objectTracker.add(value);

    Object foundValue = objectTracker.find(value);
    assertTrue(foundValue == value);
}

@Test
public void testAddingAndFindingString() {
    ObjectTracker objectTracker = new ObjectTracker();
    String value = "test";
    objectTracker.add(value);

    Object foundValue = objectTracker.find(value);
    assertTrue(foundValue == value);
}

```

The converted Kotlin code:

```
@Test
fun testAddingAndFindingInteger() {
    val objectTracker = ObjectTracker()
    val value = 1000
    objectTracker.add(value)

    val foundValue = objectTracker.find(value) // Does not find the value and so fails the assertion below...
    assertTrue(foundValue === value)
}

@Test
fun testAddingAndFindingString() {
    val objectTracker = ObjectTracker()
    val value = "test"
    objectTracker.add(value)

    val foundValue = objectTracker.find(value) // Finds the value and passes the assertion below...
    assertTrue(foundValue === value)
}

```

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [June 25, 2016, 1:24pm UTC](https://discuss.kotlinlang.org/t/equality-not-working-as-expected-with-kotlin-code-testing-java-class/1820/2 "2016-06-25T13:24:06Z")

</div>

The difference in the behavior is caused by boxing. In your Java code, you’re manually creating an instance of `java.lang.Integer` and passing it to the `add` method and to the `find` method. In your Kotlin code, `val value = 1000` defines a variable of type `Int`, which corresponds to Java’s `int`. When you call the `add` method, the `int` will be boxed into an instance of `java.lang.Integer`, and when you call `find()`, it will be boxed again so that a new instance will be created. Your code compares object identity, and therefore will not find the instance.

---

<div class="post-metadata">

**Author:** ![vbezhenar](https://avatars.discourse-cdn.com/v4/letter/v/35a633/32.png) [@vbezhenar](https://discuss.kotlinlang.org/u/vbezhenar)\
**Post date:** [June 25, 2016, 1:56pm UTC](https://discuss.kotlinlang.org/t/equality-not-working-as-expected-with-kotlin-code-testing-java-class/1820/3 "2016-06-25T13:56:41Z")

</div>

Probably Kotlin-to-Java converter should preserve java.lang.Integer type instead of replacing it with Int.

---

<div class="post-metadata">

**Author:** ![kotlinnewb](https://avatars.discourse-cdn.com/v4/letter/k/48db29/32.png) [@kotlinnewb](https://discuss.kotlinlang.org/u/kotlinnewb)\
**Post date:** [June 27, 2016, 5:52pm UTC](https://discuss.kotlinlang.org/t/equality-not-working-as-expected-with-kotlin-code-testing-java-class/1820/4 "2016-06-27T17:52:43Z")

</div>

per vbezhenar’s comment below, what would the solution be to get the Kotlin code to work without modifying the original Java?

> Probably Kotlin-to-Java converter should preserve java.lang.Integer type instead of replacing it with Int.

I can’t use this syntax in Kotlin to preserve the `java.lang.Integer` type:

```
    val i1: java.lang.Integer = 1000

```

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [June 27, 2016, 6:04pm UTC](https://discuss.kotlinlang.org/t/equality-not-working-as-expected-with-kotlin-code-testing-java-class/1820/5 "2016-06-27T18:04:26Z")

</div>

What do you mean by “without modifying the original Java”?

You can use the following declaration: `val i1: Int? = 1000`

---

<div class="post-metadata">

**Author:** ![kotlinnewb](https://avatars.discourse-cdn.com/v4/letter/k/48db29/32.png) [@kotlinnewb](https://discuss.kotlinlang.org/u/kotlinnewb)\
**Post date:** [June 27, 2016, 8:08pm UTC](https://discuss.kotlinlang.org/t/equality-not-working-as-expected-with-kotlin-code-testing-java-class/1820/6 "2016-06-27T20:08:13Z")

</div>

> What do you mean by “without modifying the original Java”?

Sorry I should have specified that in tinkering around a bit I had changed the equality check from `if (listItem == value)` to `if (listItem.equals(value))` to experiment and the tests passed. This makes sense given your comment about checking object identity. But I didn’t want to modify this original code, I was just trying it out b/c I was confused about the issue.

Thanks for the suggestion on adding `Int?` as the type, that made the test pass. If you have any extra time could you give me a brief lesson on why this fixes the auto-boxing issue you mentioned?

---

<div class="post-metadata">

**Author:** ![medium](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/medium/32/10404_2.png) [@medium](https://discuss.kotlinlang.org/u/medium)\
**Post date:** [June 27, 2016, 8:44pm UTC](https://discuss.kotlinlang.org/t/equality-not-working-as-expected-with-kotlin-code-testing-java-class/1820/7 "2016-06-27T20:44:57Z")

</div>

I think that you force the compiler to create a wrapped `Int` when declaring a nullable type (since primitive types cannot be null).

---

<div class="post-metadata">

**Author:** ![kotlinnewb](https://avatars.discourse-cdn.com/v4/letter/k/48db29/32.png) [@kotlinnewb](https://discuss.kotlinlang.org/u/kotlinnewb)\
**Post date:** [June 27, 2016, 10:21pm UTC](https://discuss.kotlinlang.org/t/equality-not-working-as-expected-with-kotlin-code-testing-java-class/1820/8 "2016-06-27T22:21:23Z")

</div>

Yup. The tests pass so it seems adding `Int?` was the solution, this makes sense because the `find` method can return `null`. But at the same time the auto-boxing aspect of this was completely unintuitive to me. That’s why I’ve asked for a quick primer on why that’s the case. @yole appreciate the help 🙂

I imagine `Int?` is creating a “wrapped Int object” so it’s boxed into an instance of a type then and there.

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [June 28, 2016, 8:25am UTC](https://discuss.kotlinlang.org/t/equality-not-working-as-expected-with-kotlin-code-testing-java-class/1820/9 "2016-06-28T08:25:51Z")

</div>

A local variable of type `Int?` will have the type `java.lang.Integer` in the bytecode, so it will behave exactly as your original Java test.

---

<div class="post-metadata">

**Author:** ![kotlinnewb](https://avatars.discourse-cdn.com/v4/letter/k/48db29/32.png) [@kotlinnewb](https://discuss.kotlinlang.org/u/kotlinnewb)\
**Post date:** [June 28, 2016, 5:20pm UTC](https://discuss.kotlinlang.org/t/equality-not-working-as-expected-with-kotlin-code-testing-java-class/1820/10 "2016-06-28T17:20:09Z")

</div>

@yole

Imagine I had a method that didn’t return `null` and I didn’t need the `Int?` type for that reason, but I still wanted to perform some instance check in the method or I wanted to check referential identity in some way. Would I then have to make it an `Int?` type anyway in order to have an instance of `Integer`?

So in my code I’d have something like:

```
val i1 = 1000 // I can't have a reference to this var...
val i2 : Int? = 1000 // I want a reference so I'll use Int?, but realistically it will never be null

```

Is this a realistic use case?

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [June 28, 2016, 5:43pm UTC](https://discuss.kotlinlang.org/t/equality-not-working-as-expected-with-kotlin-code-testing-java-class/1820/11 "2016-06-28T17:43:53Z")

</div>

Sorry, I can’t imagine any real-life code that would need to perform identity checks on boxed integers. If you have a real, non-artifical use case, I’d be happy to discuss the best way to represent it in Kotlin.

---

<div class="post-metadata">

**Author:** ![kotlinnewb](https://avatars.discourse-cdn.com/v4/letter/k/48db29/32.png) [@kotlinnewb](https://discuss.kotlinlang.org/u/kotlinnewb)\
**Post date:** [June 28, 2016, 5:48pm UTC](https://discuss.kotlinlang.org/t/equality-not-working-as-expected-with-kotlin-code-testing-java-class/1820/12 "2016-06-28T17:48:44Z")

</div>

No worries, I was just imagining and didn’t have any real example. Thanks for your help!
