1. Introduction

Boolean values in Java and Kotlin are used for decision-making statements and control structures. Boolean.valueOf() is a popular method in Java that converts a string representation of a boolean value into a Boolean object.

When working with Kotlin, we might wonder if there’s an equivalent method that works the same way. Luckily, Kotlin provides several ways to achieve the same functionality.

In this tutorial, we’re going to discuss various ways to achieve Java’s Boolean.valueOf() equivalent in Kotlin.

2. Using the toBoolean() Method

First, a simple way to achieve Java’s Boolean.valueOf() method in Kotlin involves the toBoolean() method. We can call this method on any string in Kotlin, even potentially nullable strings. It will return true if the string is not null and its content equals “true” ignoring the case difference, otherwise, it returns false:

@Test
fun `obtain equivalent in Kotlin using toBoolean method`() {
    assertEquals(true, "true".toBoolean())
    assertEquals(true, "TRUE".toBoolean())
    assertEquals(false, "false".toBoolean())
}

Specifically, we can verify if a given string translates to true or false boolean values.

2.1. Using the toBooleanStrict() Method

We can also leverage the toBooleanStrict() method to achieve Java’s equivalent of Boolean.valueOf(). The toBooleanStrict() method is case-sensitive and works for non-nullable strings. It returns true if a given string is exactly equal to the string “true” and false if the string is exactly equal to “false”, and it throws an exception if the value is anything else:

@Test
fun `obtain equivalent in Kotlin using toBooleanStrict method`() {
    assertEquals(true, "true".toBooleanStrict())
    assertEquals(false, "false".toBooleanStrict())
    assertThrows(IllegalArgumentException::class.java) {
        "TRUE".toBooleanStrict()
    }
}

In this snippet, we use the toBooleanStrict() method to show the strings “true” and “false” translate to true and false boolean values, respectively. Finally, our last assertion shows that this method throws the IllegalArgumentException for any other string.

2.2. Using the toBooleanStrictOrNull() Method

Alternatively, we can use the toBooleanStrictOrNull() method. It returns true if the content of the string is exactly equal to the string “true”, false if it is exactly equal to “false”, and null for any other values:

@Test
fun `obtain equivalent in Kotlin using toBooleanStrictOrNull method`() {
    assertEquals(true, "true".toBooleanStrictOrNull())
    assertEquals(null, "TRUE".toBooleanStrictOrNull())
    assertEquals(false, "false".toBooleanStrictOrNull())
}

In this unit test, notice that using the toBooleanStrictOrNull() for the string “TRUE” returns null since this value is not exactly equal to “true”.

3. Using the Boolean.parseBoolean() Method

Additionally, Java has a built-in method called Boolean.parseBoolean() that converts a String to a Boolean value. Fortunately, we can also use this method in Kotlin due to Kotlin-Java interoperability. Similarly, this method returns true if the string is not null and is equal to “true”, ignoring case. Otherwise, it returns false:

@Test
fun `obtain equivalent in Kotlin using parseBoolean method`() {
    assertEquals(true, java.lang.Boolean.parseBoolean("true"))
    assertEquals(true, java.lang.Boolean.parseBoolean("TRUE"))
    assertEquals(false, java.lang.Boolean.parseBoolean("false"))
}

Indeed, using the Boolean.parseBoolean() method, the strings “true” and “TRUE” return the boolean value true while the string “false” returns the boolean false.

4. Using the Boolean.valueOf() Method

Similarly, we can directly use Java’s built-in Boolean.valueOf() method in Kotlin as well, thanks to interoperability. Likewise, this method returns true if the given string is not null and is equal to “true”, ignoring case. Otherwise, it returns false:

@Test
fun `obtain equivalent in Kotlin using valueOf method`() {
    assertEquals(true, java.lang.Boolean.valueOf("true"))
    assertEquals(true, java.lang.Boolean.valueOf("TRUE"))
    assertEquals(false, java.lang.Boolean.valueOf("false"))
}

As always, we have the same set of tests that translates the strings “true” and “TRUE” to boolean value true, and “false” to boolean value false.

5. Direct String Comparison

Finally, we can use direct string comparison to convert a string to a Boolean value. That is, given any String, we check that it equals the string “true”:

fun convertStringToBoolean(input: String): Boolean {
    return if (input.equals("true", ignoreCase = true)) true 
    else false
}

In this helper method, we use the equals() method to check if a given string is equivalent to the “true” string. Furthermore, we use this method with the ignoreCase parameter to perform the comparison ignoring the case of the given string. If the given string matches the string “true”, this helper method returns true, otherwise, it returns false.

It’s always a good idea to test our code to ensure it works correctly:

@Test
fun `obtain equivalent in Kotlin by string comparison using equals method`() {
    assertEquals(true, convertStringToBoolean("true"))
    assertEquals(true, convertStringToBoolean("TRUE"))
    assertEquals(false, convertStringToBoolean("false"))
}

In this unit test, we use our helper method to easily check if a string translates to true or false using assertion statements. Note that both the strings “true” and “TRUE” translate to true, irrespective of their case differences.

5.1. Using a Custom Extension Function

We can also create a custom extension function that converts a string representation of a boolean value into a Boolean object:

fun String.toBooleanValue(): Boolean =
    when (this.toLowerCase()) {
        "true" -> true
        else -> false
    }

This extension function returns a boolean value. We bypass the natural case sensitivity of the when expression by lowercasing the string first and then checking equality to the string “true”.

Let’s test our extension function as well:

@Test
fun `obtain equivalent in Kotlin using custom extension function`() {
    assertEquals(true, "true".toBooleanValue())
    assertEquals(true, "TRUE".toBooleanValue())
    assertEquals(false, "false".toBooleanValue())
}

5.2. Using Regular Expressions

Lastly, we can also use regular expressions to convert a string to a Boolean value:

fun convertStringToBooleanUsingRegex(input: String): Boolean {
    return input.trim().matches("^(?i:true)$".toRegex())
}

In this helper method, we use regex to check if the input string matches “true” (case-insensitive). If the input string matches the string “true”, our helper method returns true, else, it returns false. 

First, we use the trim() method to remove any leading or trailing whitespace on the string. Here’s the step-by-step breakdown of the regex we used:

  •  The starting ^ and ending $ means we expect to match the whole string with our regex.
  • (?i:true) is looking for the string “true”, configured with the case-insensitive modifier i.

Let’s now test our helper method:

@Test
fun `obtain equivalent in Kotlin using regex`() {
    assertEquals(true, convertStringToBooleanUsingRegex("true"))
    assertEquals(true, convertStringToBooleanUsingRegex("TRUE"))
    assertEquals(false, convertStringToBooleanUsingRegex("false"))
}

So, we can ascertain that the strings “true” and “TRUE” translate to true, while the string “false” translates to false.

6. Conclusion

In this article, we’ve covered various ways to achieve the equivalent of Java’s Boolean.valueOf() method in Kotlin. In brief, we’ve explored direct string comparison approaches, built-in functions, and techniques that leverage Java’s interoperability with Kotlin. We are encouraged to adopt any of these approaches in our projects according to our specific needs.

As always, the complete source code used in this article is available over on GitHub.
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.