Kotlin Data Types šŸŽÆ

beginner
12 min

Kotlin Data Types šŸŽÆ

Welcome to the Kotlin Data Types tutorial! In this lesson, we'll explore the basic data types that Kotlin offers, and understand how to use them effectively in your projects. Let's get started! šŸš€

What are Data Types in Kotlin? šŸ“

In programming, data types define the nature of data that a variable can store. Kotlin supports various data types, including:

  • Primitive data types
    • Boolean
    • Char
    • Int
    • Float
    • Double
    • String

Boolean šŸ’”

The Boolean data type represents two possible values: true or false. It's useful when dealing with conditions and decisions in your code.

kotlin
val isValid: Boolean = true

šŸ“ Pro Tip: The == operator is used to compare Boolean values.

Quick Quiz
Question 1 of 1

Which operator is used to compare `Boolean` values in Kotlin?


Char šŸ’”

The Char data type represents a single character, such as 'A' or ' '. Characters are enclosed in single quotes.

kotlin
val firstLetter: Char = 'A'

šŸ“ Pro Tip: Use the \u escape sequence to include Unicode characters, like this: '\u00A9' for the copyright symbol (Ā©).


Int šŸ’”

The Int data type is used for whole numbers. It has a maximum value of 2147483647 and a minimum value of -2147483648.

kotlin
val age: Int = 30

šŸ“ Pro Tip: To work with larger or smaller integers, use the Long data type.


Float and Double šŸ’”

The Float and Double data types are used for real numbers, like decimal values.

kotlin
val price: Float = 19.99F // Note the 'F' at the end to specify Float val weight: Double = 87.5

šŸ“ Pro Tip: Use Float for smaller decimal values, and Double for larger ones, as Double offers greater precision.


String šŸ’”

The String data type is used to store text. Strings are defined by wrapping text in double quotes.

kotlin
val name: String = "John Doe"

šŸ“ Pro Tip: To include a double quote inside a string, escape it by using a backslash: \".


Practice Time! šŸŽÆ

To test your understanding, let's solve a simple problem:

kotlin
fun main() { val age: Int = 30 val name: String = "John Doe" // Your code here }
  1. Print John Doe's age.
  2. Determine if John Doe is over 25 years old.

Good luck! šŸ¤ž


Stay tuned for the next lesson on Kotlin Variables and Operators! šŸš€

Next Lesson: Kotlin Variables and Operators šŸŽÆ