100 Days of Kotlin — Day 2:Data Types — The Building Blocks of Variables

Rohan Ashlesh
4 min readJun 17, 2023

--

Welcome back to our 100 Days of Learning Kotlin! In Day 1, we laid the groundwork by setting up our development environment and getting acquainted with the basics of Kotlin. Today, we’ll take a closer look at Kotlin’s data types, which form the foundation for storing and manipulating information in variables. So, let’s dive into the world of data types and expand our coding toolkit!

Kotlin: The Swiss Army Knife of Languages

Before we dive into the nitty-gritty of Kotlin, let’s take a moment to appreciate its versatility. Kotlin is often hailed as the “Swiss Army Knife” of programming languages, as it combines the best features of object-oriented and functional programming. It provides a concise and expressive syntax that allows developers to write cleaner, more readable code. So, let’s unwrap this coding Swiss Army Knife and see what Kotlin has in store for us!

val greeting: String = "Hello, Kotlin!"

Here, we declare a variable named greeting of type String. The val keyword indicates that it's an immutable (read-only) variable. So, once we assign a value, we cannot change it.

Did you hear about the programmer who got locked out? They forgot their val!

Just a little programming humor to keep things light! Now, let’s explore more about Kotlin’s variables.

Functions: Your Coding Superpower

Functions are the building blocks of any programming language. In Kotlin, defining a function is as easy as ABC. Check out this example:

fun greet(name: String) {
println("Hello, $name!")
}

Why did the programmer break up with their function? It returned Null.

Ah, programming humor never gets old! Now, let’s move on to Data Types.

Data Types: The Essence of Variables

In programming, data types define the kind of values that can be stored in variables. Kotlin provides a rich set of built-in data types, allowing us to handle a wide range of data with precision and flexibility. Let’s explore some of the key data types in Kotlin and understand how they work.

1. Numbers: Counting the Possibilities

Numbers are an essential part of any programming language. Kotlin supports various numerical data types, including integers and floating-point numbers.

  • Integers: Integers represent whole numbers without decimal places. Kotlin provides different integer types based on their range and memory usage. For example:
val age: Int = 25

In this example, we declare an age variable of type Int and assign it a value of 25. Integers are suitable for representing quantities, indices, and many other numeric values.

  • Floating-Point Numbers: Floating-point numbers represent numbers with decimal places. Kotlin offers two floating-point types: Float and Double. Here's an example:
val pi: Double = 3.14159

In this snippet, we declare a pi variable of type Double and assign it the value of π (approximately 3.14159). Floating-point numbers are useful for handling calculations involving decimal values.

2. Booleans: True or False

Booleans represent logical values of either true or false. They're commonly used for decision-making and conditional expressions. In Kotlin, we use the Boolean data type to define boolean variables. Let's see an example:

val isSunny: Boolean = true

In this case, we declare an isSunny variable of type Boolean and set it to true. Booleans help us control the flow of our programs and make decisions based on conditions.

3. Strings: Expressing Textual Delights

Strings allow us to work with textual data such as names, messages, and more. Kotlin supports strings using the String data type. Let's look at an example:

val message: String = "Hello, Kotlin!"

Here, we declare a message variable of type String and assign it the value "Hello, Kotlin!". Strings are invaluable for displaying text and manipulating textual information in our programs.

4. More Data Types to Discover

In addition to numbers, booleans, and strings, Kotlin offers a range of other data types, including characters (Char), arrays (Array), collections (List, Set, Map), and more. We'll explore these in detail in future posts, so stay tuned!

Congratulations on completing Day 2 of our Kotlin learning journey! Today, we explored some of Kotlin’s fundamental data types, which serve as the building blocks for storing and manipulating information in variables. By understanding these data types, we can create more powerful and expressive programs.

In the next post, we’ll dive deeper into variables and explore the concept of mutability. We’ll learn how to create mutable and immutable variables and examine the implications of each approach.

Keep coding, exploring, and expanding your Kotlin expertise

--

--

Rohan Ashlesh
Rohan Ashlesh

Written by Rohan Ashlesh

Product | Consultant | Writer | Business Enthusiast. Anything growth, I'm your guy.