This article explores different ways to pass an integer by reference in Kotlin.

1. Using a Wrapper class

The idea is to create a reference class that contains the integer as a member field. This function is demonstrated below:

Download Code

2. Using an Array

We can also use an array of length one to wrap an integer.

Download Code

3. Using AtomicInteger

Alternately, you can use the AtomicInteger class, which offers several useful functions for manipulating an integer.

Download Code

That’s all about passing an integer by reference in Kotlin.