# Kotlin switch 2 integers

**URL:** <https://discuss.kotlinlang.org/t/kotlin-switch-2-integers/6629>\
**Category:** Language Design\
**Created:** [February 14, 2018, 9:44am UTC](https://discuss.kotlinlang.org/t/kotlin-switch-2-integers/6629 "2018-02-14T09:44:34Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cosmoff](https://avatars.discourse-cdn.com/v4/letter/c/b5e925/32.png) [@cosmoff](https://discuss.kotlinlang.org/u/cosmoff)\
**Post date:** [February 14, 2018, 9:44am UTC](https://discuss.kotlinlang.org/t/kotlin-switch-2-integers/6629/1 "2018-02-14T09:44:34Z")

</div>

Hello,  
I would like switch 2 integers. With Java I could do that thank to :  
`void switch( int &a, int &b){ int memory = a; a = b; b = a; }`

but how can I do that with kotlin  
thanks for your help

---

<div class="post-metadata">

**Author:** ![jstuyts](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jstuyts/32/2141_2.png) [@jstuyts](https://discuss.kotlinlang.org/u/jstuyts)\
**Post date:** [February 14, 2018, 9:49am UTC](https://discuss.kotlinlang.org/t/kotlin-switch-2-integers/6629/2 "2018-02-14T09:49:58Z")

</div>

This code is not Java. And your code does not work. Please take some time to ensure that your question is clear and correct before posting. Thank you.

---

<div class="post-metadata">

**Author:** ![cosmoff](https://avatars.discourse-cdn.com/v4/letter/c/b5e925/32.png) [@cosmoff](https://discuss.kotlinlang.org/u/cosmoff)\
**Post date:** [February 14, 2018, 9:57am UTC](https://discuss.kotlinlang.org/t/kotlin-switch-2-integers/6629/3 "2018-02-14T09:57:09Z")

</div>

yes, i did an error when I copied :  
void change( int &a, int &b){ int memory = a; a = b; b = memory; }

---

<div class="post-metadata">

**Author:** ![jstuyts](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jstuyts/32/2141_2.png) [@jstuyts](https://discuss.kotlinlang.org/u/jstuyts)\
**Post date:** [February 14, 2018, 10:02am UTC](https://discuss.kotlinlang.org/t/kotlin-switch-2-integers/6629/4 "2018-02-14T10:02:26Z")

</div>

Still not Java.

But to answer your question: This is not possible in Kotlin unless you wrap `Int`s with a mutable wrapper:

```auto
class MutableInt(var value: Int)

fun switch(a: MutableInt, b: MutableInt) {
    val memory = a.value
    a.value = b.value
    b.value = memory
}

```

---

<div class="post-metadata">

**Author:** ![forinil2](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/forinil2/32/2465_2.png) [@forinil2](https://discuss.kotlinlang.org/u/forinil2)\
**Post date:** [February 14, 2018, 10:04am UTC](https://discuss.kotlinlang.org/t/kotlin-switch-2-integers/6629/5 "2018-02-14T10:04:31Z")

</div>

First, this:

```cpp
void change( int &a, int &b){ int memory = a; a = b; b = memory; }

```

as far as I can tell is C++ not Java.

Unless I am mistaken, Java equivalent would be:

```java
static void change(Integer a, Integer b) {
  Integer memory = a;
  a = b; 
  b = memory;
}

```

And in Kotlin (since `Int?` is stored as `Integer` and not primitive `int`: [https://kotlinlang.org/docs/reference/basic-types.html](https://kotlinlang.org/docs/reference/basic-types.html)):

```kotlin
static void change(Int? a, Int? b) {
  val memory: Int? = a;
  a = b; 
  b = memory;
}

```

---

<div class="post-metadata">

**Author:** ![jstuyts](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jstuyts/32/2141_2.png) [@jstuyts](https://discuss.kotlinlang.org/u/jstuyts)\
**Post date:** [February 14, 2018, 10:08am UTC](https://discuss.kotlinlang.org/t/kotlin-switch-2-integers/6629/6 "2018-02-14T10:08:58Z")

</div>

> [@forinil2](#):
>
> Unless I am mistaken, Java equivalent would be:
> 
> ```java
> static void change(Integer a, Integer b) {
> Integer memory = a;
> a = b; 
> b = memory;
> }
> 
> ```
> 
> And in Kotlin (since Int? is stored as Integer and not primitive int: [https://kotlinlang.org/docs/reference/basic-types.html](https://kotlinlang.org/docs/reference/basic-types.html)):
> 
> ```auto
> static void change(Int? a, Int? b) {
> val memory: Int? = a;
> a = b; 
> b = memory;
> }
> 
> ```

Both of your code samples do not work, but for different reasons:

- The Java version only changes the parameters `a` and `b`, but not the values they point to.
- Parameters are immutable in Kotlin, so you cannot assign to them. And even if you could, you would have the same problem as with Java.

Please make sure your code samples are correct before posting. If in doubt, verify the behavior in a short prorgram before posting. Thank you.

---

<div class="post-metadata">

**Author:** ![forinil2](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/forinil2/32/2465_2.png) [@forinil2](https://discuss.kotlinlang.org/u/forinil2)\
**Post date:** [February 14, 2018, 10:17am UTC](https://discuss.kotlinlang.org/t/kotlin-switch-2-integers/6629/7 "2018-02-14T10:17:33Z")

</div>

Thanks for pointing my mistakes. I’ll be more careful posting next time.

---

<div class="post-metadata">

**Author:** ![cosmoff](https://avatars.discourse-cdn.com/v4/letter/c/b5e925/32.png) [@cosmoff](https://discuss.kotlinlang.org/u/cosmoff)\
**Post date:** [February 14, 2018, 10:38am UTC](https://discuss.kotlinlang.org/t/kotlin-switch-2-integers/6629/8 "2018-02-14T10:38:16Z")

</div>

thank you jstuyts it works

---

<div class="post-metadata">

**Author:** ![aug828](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@aug828](https://discuss.kotlinlang.org/u/aug828)\
**Post date:** [February 15, 2018, 10:11pm UTC](https://discuss.kotlinlang.org/t/kotlin-switch-2-integers/6629/9 "2018-02-15T22:11:45Z")

</div>

I think this is simpler:

`a = b.also { b = a}`
