## 1. Overview

Boolean is a fundamental data type. It can have only true and false values.

In this quick tutorial, we’ll explore how to convert a Boolean instance to an Int, for example, true to 1 and false to 0.

## 2. Introduction to the Problem

Sometimes, converting a Boolean instance to an Int may simplify some calculations. For example, we’re given a list of Boolean objects and want to count how many “true“s are in the list.

If we could convert all “true”s to 1 and “false”s to 0, we can quickly get the number of “true“s by calling sum() on the Int list.

Next, we’ll see two ways to do the conversion. Also, for simplicity, we’ll use unit test assertions to verify the result.

## 3. Using Kotlin Extensions

Implementing the Boolean to Int conversion logic isn’t a challenge for us. We can easily create a helper function to do that:

``fun booleanToInt(b: Boolean) = if (b) 1 else 0``

However, this may break the fluency of the function calls. For example, let’s imagine that the Boolean argument b is calculated using other complex function calls. Here’s one style of calling this booleanToInt() function:

``booleanToInt(complex().calls().to().getBoolean())``

Alternatively, we could call it via a variable:

``````val b = complex().calls().to().getBoolean()
booleanToInt(b)``````

One great Kotlin feature is Kotlin Extension. It allows us to extend a class or interface by adding new functionalities without inheriting them. Further, extensions make the code easier to read.

For example, we can extend the standard Boolean class by adding an extension function:

``fun Boolean.toInt() = if (this) 1 else 0``

Now, we can call the toInt() function fluently: complex().calls().to().getBoolean().toInt().

Next, let’s write a simple test to see if it works as expected:

``````assertThat(true.toInt()).isEqualTo(1)
assertThat(false.toInt()).isEqualTo(0)``````

Unsurprisingly, the test passes if we run it.

Apart from extension functions, Kotlin allows us to create extension properties. So next, let’s add a new property intValue to the Boolean class:

``````val Boolean.intValue
get() = if (this) 1 else 0``````

Then, we can access the intValue property as regular properties:

``````assertThat(true.intValue).isEqualTo(1)
assertThat(false.intValue).isEqualTo(0)``````

As we can see from the examples, using extensions can make our code easier to read.

## 4. Using the compareTo() Function

Probably, many of us may haven’t noticed that the Boolean class implements the Comparable interface:

``public class Boolean private constructor() : Comparable<Boolean> { ...``

This implies that we can compare two Boolean objects. In Kotlin, we can even use the “greater than (>)”  or the “less than (<)” operators to compare two booleans, although we seldom do that:

``assertThat(true > false).isTrue``

The test above passes. So, in Kotlin, true is “greater than” false.

Further, we can use b.compareTo(false) to convert the b object to an Int. If the b value is true, b.compareTo(false) will return 1, otherwise, the method returns 0. In this way, we convert the Boolean b to an integer.

Let’s create a test to check if it works as expected:

``````assertThat(true.compareTo(false)).isEqualTo(1)
assertThat(false.compareTo(false)).isEqualTo(0)``````

If we give it a run, the test passes. So, the booleans are converted to expected integers.

Sharp eyes may spot that we compare the boolean value to false. We may ask, why don’t we compare it to true? This is because, assuming a and b are two Boolean objects, a.compareTo(b) follows this rule:

• a>b: it returns 1
• a=b: it returns 0
• a<b: it returns -1

As we’ve mentioned, true > false, so if we compare to the smaller value (false), the result will be 0 or 1.

However, if we compare a boolean to true, say b.compareTo(true), the result would be 0 or -1:

``````assertThat(true.compareTo(true)).isEqualTo(0)
assertThat(false.compareTo(true)).isEqualTo(-1)``````

The b.compareTo(false) approach might not be as straightforward as previously addressed Kotlin extensions. However, we don’t need to add additional functions to do the conversion.

## 5. Conclusion

In this article, we’ve learned two approaches to converting a Boolean object to an Int through examples.

First, we’ve addressed Kotlin’s extension way to solve the problem. It can make the function call fluent also easy to read. However, writing extensions for this task in every project could be a bit tedious. So, we’ve discussed another interesting approach: converting Boolean to Int using the compareTo(false) method.

As always, the full source code used in the article can be found over on GitHub.