Understanding Basic Types and Expressions in Kotlin
Kotlin is a modern, statically-typed programming language that is built on the JVM. It combines the best features from languages like Java, Ruby, and JavaScript while introducing its own unique elements. One of the first things any programmer needs to grasp when learning a new language is how data types work. In Kotlin, basic types include integers, floating-point numbers, booleans, strings, and characters.
Everything is an Object
One of Kotlin’s unique features is that everything is an object. This stands in contrast to languages like Java, where primitive types like int
and char
are not objects but can be wrapped in their corresponding wrapper classes (e.g., Integer
, Character
). In Kotlin, even numbers like 1
are objects. This simplifies the language by doing away with the distinction between primitives and objects.
Numeric Types in Kotlin
Kotlin supports four integral types:
Byte
: 8-bitShort
: 16-bitInt
: 32-bitLong
: 64-bit
Additionally, Kotlin supports two floating-point types:
Float
: 32-bitDouble
: 64-bit
You can define numeric literals directly, and Kotlin will infer the type. However, you can also specify types explicitly:
1234
is anInt
1234L
is aLong
3.14159
is aDouble
3.14159f
is aFloat
Number Literals and Formatting
Kotlin supports various number literals. For better readability, you can use underscores (_
) to separate groups of digits in large numbers, such as:
This makes it easier to read numbers without affecting their value. You can also define numbers in different bases:
- Hexadecimal:
0xFFA4C639
- Binary:
0b10110100
Mathematical Operations
Kotlin provides standard mathematical operations like addition (+
), subtraction (-
), multiplication (*
), division (/
), and modulus (
Working with Booleans
Kotlin has a Boolean
type with two values: true
and false
. It also provides the standard Boolean operators:
&&
: Logical AND
||
: Logical OR
!
: Logical NOT
Strings in Kotlin
Strings are a staple in most programming languages, and Kotlin provides several ways to define strings:
- Double-quoted strings: These are similar to strings in Java and can contain escape sequences like
\n
for newlines or \t
for tabs.
- Triple-quoted strings: These are used for raw strings, where newlines and special characters are preserved without needing escape sequences:
This results in:
You can also remove unwanted indentation from raw strings using the trimMargin()
function:
This will remove the leading whitespace, making the string more readable.
String Concatenation and Interpolation
Kotlin allows string concatenation with the +
operator:
However, Kotlin also supports string interpolation, which makes string concatenation cleaner and more intuitive. You can directly embed expressions in string literals using $
:
Characters in Kotlin
A character in Kotlin is represented by a single-quoted literal, just like in Java:
Characters can also use escape sequences like \n
for newlines or \t
for tabs.
Equality Checks
In most languages, the ==
operator checks for equality. In Kotlin, ==
is used for content equality, while ===
checks for instance equality (whether two references point to the same object).
Conclusion
Kotlin simplifies a lot of the complexities present in other languages, making it easier to work with basic types and expressions. With powerful features like string interpolation, raw strings, and the lack of distinction between primitive types and objects, Kotlin becomes an excellent choice for modern software development. Whether you’re working on Android apps or server-side code, understanding these basic types and expressions is key to getting started with Kotlin.