kotlin
  1. kotlin-data-type

Kotlin Data Types

Kotlin Data Types are the basic building blocks of any program. They are used to define the type of data that can be stored in a variable. Kotlin provides support for a wide range of data types, including primitive and reference types.

Syntax

The syntax for declaring a variable in Kotlin is var identifier: data_type = value.

For example, to declare an integer variable with a value of 10, the syntax would be:

var age: Int = 10

Example

fun main(args: Array<String>) {
    var name: String = "John"
    var age: Int = 25
    var isMarried: Boolean = false
    var height: Double = 6.1

    println("Name: "+name)
    println("Age: "+age)
    println("Married: "+isMarried)
    println("Height: "+height)
}

Output

Name: John
Age: 25
Married: false
Height: 6.1

Explanation

In the example above, we have declared four variables of data types String, Int, Boolean, and Double. We have initialized these variables with values and printed their values using the println() function.

The String data type is used to store a sequence of characters, such as names, addresses, etc. The Int data type is used to store integer values, such as age, numbers, etc. The Boolean data type is used to store true or false values, such as isMarried, isActive, etc. The Double data type is used to store decimal values, such as height, weight, etc.

Use

Data types are used to store values of different types. They help programmers to create more complex programs by providing a structured way to store and manipulate data.

Important Points

  • Kotlin supports a wide range of data types, including primitive and reference types.
  • Data types are used to define the type of data that can be stored in a variable.
  • The Kotlin data types include int, double, float, long, short, byte, char, Boolean, and String.
  • Variables can be assigned values during their declaration or later in the program.
  • The value assigned to a variable must be of the same data type as the variable.

Summary

Data types are an essential part of any programming language. They help programmers to create more complex programs by providing a structured way to store and manipulate data. Kotlin supports a wide range of data types, including primitive and reference types. Understanding data types is critical for anyone learning how to program in Kotlin.

Published on: