In Kotlin,Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
Boolean is a fundamental data type representing a logical entity that can possess one of exactly two values: true or false. On the JVM, a non-nullable Boolean is compiled to a primitive boolean to optimize memory and performance, while a nullable Boolean? is boxed as a java.lang.Boolean object reference. To maintain primitive memory optimization for arrays on the JVM, Kotlin provides the BooleanArray type, which compiles to a primitive boolean[]. In contrast, using a standard Array<Boolean> results in an array of boxed objects (Boolean[]).
Declaration and Instantiation
Boolean variables and arrays can be declared explicitly or inferred by the compiler.Logical Operators
Kotlin provides standard logical operators forBoolean types. The conjunction and disjunction operators utilize short-circuit evaluation, meaning the right-hand operand is only evaluated if the left-hand operand does not definitively determine the outcome.
||(Logical OR): Returnstrueif at least one operand istrue. Short-circuits if the left operand istrue.&&(Logical AND): Returnstrueif both operands aretrue. Short-circuits if the left operand isfalse.!(Logical NOT): A unary operator that inverts the boolean value.
Infix Functions
In addition to standard operators, Kotlin provides infix functions for boolean arithmetic. Unlike&& and ||, these infix functions do not short-circuit; both operands are always evaluated.
and: Performs a logical AND operation.or: Performs a logical OR operation.xor: Performs a logical exclusive OR operation. Returnstruestrictly when the operands evaluate to different values.
Nullability and Equality
When working with a nullableBoolean?, standard logical operators (&&, ||, !) cannot be used directly without prior null-checking or unwrapping. However, structural equality operators (==, !=) are null-safe.
String Conversion
The KotlinString API provides extension functions to parse string values into Boolean types. These functions offer varying levels of strictness regarding case sensitivity and format validation.
toBoolean(): Case-insensitive and lenient. Returnstrueif the string is equal to"true"(ignoring case), and safely returnsfalsefor any other string, including invalid formats.toBooleanStrict(): Strictly case-sensitive. Returnstruefor"true"andfalsefor"false", but throws anIllegalArgumentExceptionfor any other value.toBooleanStrictOrNull(): Strictly case-sensitive, but returnsnullinstead of throwing an exception for invalid formats.
Master Kotlin with Deep Grasping Methodology!Learn More





