codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🎯

Kotlin

4 / 68 topics
1Introduction to Kotlin2Kotlin Installation3Hello World Program4Variables and Data Types5Operators in Kotlin6Control Flow Statements7Functions in Kotlin8Strings and Templates9Collections in Kotlin10Null Safety
Tutorials/Kotlin/Variables and Data Types
🎯Kotlin

Variables and Data Types

Updated 2026-04-20
3 min read

Introduction

Variables and data types are fundamental concepts in any programming language, including Kotlin. Understanding how to declare variables, choose appropriate data types, and manipulate them is crucial for effective coding. In this tutorial, we will explore the basics of variables and data types in Kotlin, providing real-world examples and best practices.

Variables in Kotlin

Variables in Kotlin are declared using the var keyword for mutable variables (which can be changed) or the val keyword for immutable variables (which cannot be changed after initialization).

Declaring Variables

To declare a variable, you specify its type followed by the variable name and assign it an initial value. Here’s how you do it:

var name: String = "Alice"
val age: Int = 30

In this example:

  • name is a mutable string variable initialized with "Alice".
  • age is an immutable integer variable initialized with 30.

Type Inference

Kotlin supports type inference, which means you can omit the type if it can be inferred from the context:

var name = "Alice" // Kotlin infers that 'name' is of type String
val age = 30       // Kotlin infers that 'age' is of type Int

Variable Naming Conventions

  • Use camelCase for variable names.
  • Start with a lowercase letter.
  • Avoid using reserved keywords as variable names.

Data Types in Kotlin

Kotlin has several built-in data types, which can be categorized into primitive and reference types.

Primitive Types

Primitive types are the basic building blocks of data. Kotlin provides the following primitive types:

  • Byte (8-bit signed integer)
  • Short (16-bit signed integer)
  • Int (32-bit signed integer)
  • Long (64-bit signed integer)
  • Float (32-bit IEEE 754 floating point number)
  • Double (64-bit IEEE 754 floating point number)
  • Char (16-bit Unicode character)
  • Boolean (true or false)

Reference Types

Reference types are objects that are stored in the heap memory. Kotlin provides several reference types, including:

  • String
  • Arrays
  • Collections (e.g., List, Set, Map)

Strings

Strings in Kotlin are immutable sequences of characters. You can create strings using either single quotes or double quotes:

val greeting: String = "Hello, Kotlin!"
val anotherGreeting: String = 'Hello, Kotlin!'

Arrays

Arrays in Kotlin are fixed-size collections of elements of the same type. You can declare an array using the arrayOf function:

val numbers: Array<Int> = arrayOf(1, 2, 3, 4, 5)

Collections

Kotlin provides a rich set of collection types, including lists, sets, and maps.

  • List: An ordered collection that allows duplicate elements.

    val fruits: List<String> = listOf("Apple", "Banana", "Cherry")
    
  • Set: A collection that does not allow duplicate elements.

    val uniqueNumbers: Set<Int> = setOf(1, 2, 3, 4, 5)
    
  • Map: A collection of key-value pairs.

    val personInfo: Map<String, Any> = mapOf("name" to "Alice", "age" to 30)
    

Type Conversion

Kotlin provides several ways to convert between different data types.

Implicit Conversion

Implicit conversion is automatically performed by the compiler when it's safe to do so. For example:

val a: Int = 10
val b: Double = a.toDouble() // Implicit conversion from Int to Double

Explicit Conversion

Explicit conversion requires manual casting and can lead to runtime errors if not handled properly. Use the to functions for safe conversions:

val d: Double = 123.45
val i: Int = d.toInt() // Explicit conversion from Double to Int, truncating the decimal part

Best Practices

  • Use Immutable Variables (val) by Default: Prefer val over var whenever possible to make your code safer and more predictable.

    val immutableValue = "This cannot be changed"
    
  • Avoid Nullability Issues: Kotlin has a strong null safety feature. Use nullable types (Type?) only when necessary and handle nulls properly using safe calls or elvis operator.

    var nullableString: String? = null
    val length = nullableString?.length ?: 0 // Safe call with default value
    
  • Use Type Aliases for Clarity: If you have complex types, consider using type aliases to improve code readability.

    typealias EmailAddress = String
    val email: EmailAddress = "example@example.com"
    

Conclusion

Variables and data types are the backbone of any Kotlin program. Understanding how to declare, use, and convert variables is essential for writing efficient and safe code. By following best practices and leveraging Kotlin's powerful features, you can write robust applications that are easy to maintain and extend.

In the next section, we will explore control flow structures in Kotlin, including conditional statements and loops. Stay tuned!


PreviousHello World ProgramNext Operators in Kotlin

Recommended Gear

Hello World ProgramOperators in Kotlin