In Kotlin, data types like String
, Int
, and Boolean
are fundamental types that are used to work with text, numbers, and logical values respectively. Kotlin provides these types as part of its standard library, and they are straightforward to use. Below is an explanation of how to work with them:
1. String
A String
in Kotlin represents a sequence of characters. You can create and manipulate strings easily.
Example:
fun main() {
val name: String = "Kotlin"
val greeting = "Hello, $name!" // String interpolation
val length = name.length // Access string property
println(greeting) // Output: Hello, Kotlin!
println("Length of name: $length") // Output: Length of name: 6
// Multi-line string
val multiLineString = """
This is a
multi-line string.
""".trimIndent()
println(multiLineString)
}
Key Features of Strings:
- String interpolation (
$
) to include variables or expressions within a string. - Supports multi-line strings using triple quotes (
"""
). - String manipulation methods, e.g.,
.length
,.substring()
,.toUpperCase()
, etc.
2. Int
An Int
is a basic data type representing a 32-bit integer in Kotlin. It is used for whole numbers.
Example:
fun main() {
val number: Int = 42
val doubled = number * 2
val isEven = number % 2 == 0
println("Number: $number") // Output: Number: 42
println("Doubled: $doubled") // Output: Doubled: 84
println("Is even? $isEven") // Output: Is even? true
}
Key Points:
Int
is one of the numeric data types, which also includeLong
,Short
,Byte
,Double
, andFloat
.- Kotlin handles mathematical operations with
Int
and other numeric types directly. - Reading and writing numbers are simple, and type conversion can be performed using
.toInt()
,.toDouble()
, etc.
3. Boolean
A Boolean
in Kotlin represents a value that is either true
or false
.
Example:
fun main() {
val isKotlinFun: Boolean = true
val isJavaFun = false
println("Is Kotlin fun? $isKotlinFun") // Output: Is Kotlin fun? true
println("Is Java fun? $isJavaFun") // Output: Is Java fun? false
// Logical operations
val bothFun = isKotlinFun && isJavaFun // AND operation
val eitherFun = isKotlinFun || isJavaFun // OR operation
println("Both fun? $bothFun") // Output: Both fun? false
println("Either fun? $eitherFun") // Output: Either fun? true
}
Key Points:
- Booleans are used for logical operations and for controlling conditions in
if
statements, loops, etc. - Supports logical operators:
&&
(AND),||
(OR),!
(NOT).
Type Inference
In Kotlin, thanks to type inference, you don’t always need to explicitly specify the type of a variable. The compiler can infer the type based on the assigned value.
Example:
fun main() {
val name = "Kotlin" // Automatically inferred as String
val age = 25 // Automatically inferred as Int
val isActive = true // Automatically inferred as Boolean
println(name)
println(age)
println(isActive)
}
Additional Tips
- Type Conversion: When converting between data types (e.g.,
String
toInt
), use methods liketoInt()
,toDouble()
, etc.val number = "123".toInt() // Converts String to Int val decimal = 3.14.toString() // Converts Double to String
- Null Safety: These types are non-null by default. If you want a variable to hold a
null
value, use the nullable types (e.g.,String?
,Int?
,Boolean?
).val nullableString: String? = null
With those basics, you are ready to work with String
, Int
, and Boolean
in Kotlin!