In Kotlin, everything is an object in the sense that you can call member
functions and properties on any variable. While certain types have an
optimized internal representation as primitive values at runtime (such as
numbers, characters, booleans and others), they appear and behave like regular
classes to you.
Basic types, Type checks, and casts used in Kotlin |
Basic Types
Let's talk about some basic types in Kotlin. In programming, types help us
understand what kind of data we're working with. Kotlin, like many programming
languages, has several basic types. Here are some of the most common ones:
- Numbers and their unsigned counterparts
- Booleans
- Characters
- Strings
- Arrays
1. Numbers
Integers (Int):
Integers represent whole numbers without any decimal points. They can be positive, negative, or zero. In Kotlin, theInt
type is used to
declare integer variables.
val myAge: Int = 30 val temperature: Int = -10
Floating Point Numbers (Double and Float):
Floating-point numbers represent numbers with fractional parts. Kotlin provides two types for floating-point numbers:- Double: Represents double-precision floating-point numbers. It's the default type for floating-point literals.
- Float: Represents single-precision floating-point numbers. If you need to specify a floating-point literal as Float, you need to add 'f' or 'F' at the end (less accurate but takes less memory).
val pi: Double = 3.14 val gravity: Float = 9.8f // Notice the 'f' at the end to specify it's a Float
2. Boolean (Boolean)
Booleans represent true or false values. They are commonly used for conditions
and logic in programming.
val isSunny: Boolean = true val isLoggedIn: Boolean = false
3. Characters (Char)
Characters represent single letters, digits, or other special symbols enclosed
in single quotes.
val firstLetter: Char = 'A' val digit: Char = '7'
4. Strings (String)
Strings are sequences of characters. In Kotlin, they are immutable, meaning
their values cannot be changed after they are created.
val message: String = "Hello, Kotlin!" val name: String = "Alice"
5. Arrays
Arrays are collections of similar items. In Kotlin, you define arrays using
the
arrayOf()
function
val numbers: Array<Int> = arrayOf(1, 2, 3, 4, 5) val colors = Array(3) { i -> "Color $i" } // Array of Strings with size 3
Type checks and casts
In Kotlin, type checks and casts are mechanisms used to ensure type safety and
facilitate working with different data types in the code. Here's an
explanation of each:
1. Type Checks (is operator):
The is operator is used in Kotlin to check whether an object is of a certain
type. It returns true if the object is an instance of the specified type or
one of its subtypes; otherwise, it returns false.
val obj: Any = "Hello" if (obj is String) { println(obj.length) // Safe to access length property of String } if (obj !is String) { // Same as !(obj is String) print("Not a String") } else { print(obj.length) }
In this example, the is operator checks if
obj
is of type
String
. If it is, then it's safe to access properties or
methods specific to the String
type within the corresponding block.
2. Casts
Casting is the process of converting an object from one type to another. In
Kotlin, you can cast an object to another type using the as operator. There
are two types of casts: smart casts and explicit casts.
Smart Casts
Kotlin provides smart casts, which automatically cast an object to the
checked type within the scope where the type check was performed. It
eliminates the need for explicit casting and makes the code cleaner and
safer.
fun example(obj: Any) { if (obj is String) { println(obj.length) // Smart cast: obj is automatically cast to String } }
Explicit Casts
Explicit casts are used when the compiler cannot infer the casting
automatically, typically when working with nullable types or when casting to
a supertype. They are performed using the as operator.
val obj: Any? = "Hello" val length = if (obj is String) { obj.length // Smart cast: obj is automatically cast to String } else { (obj as? Int) ?: 0 // Explicit cast to Int or default to 0 if null }
In this example, if
obj
is of type String
, it is
automatically smart cast to String
, and its length property is
accessed safely. If it's not a String
, it tries to cast it to
Int
using the safe cast operator as?. If the cast is
successful, it returns the length; otherwise, it returns 0.
Other Topics:
Thanks for reading this article. Hope you would have liked it!. Please share and subscribe to my blog to support.