Data Types in Kotlin Programming language

0

 

Data Types in Kotlin Programming language

Data Types in Kotlin

In Kotlin, data types specify what kind of data a variable can hold. Each data type serves a specific purpose, allowing you to store different kinds of values. Here’s a list of the main data types in Kotlin:

List of Kotlin Data Types:

  • Integer Types: Byte, Short, Int, Long
  • Floating-Point Types: Float, Double
  • Character Type: Char
  • Boolean Type: Boolean
  • String Type: String
  • Array Type: Array

Now, let’s go through each data type one by one, with examples.

1. Integer Types

Definition: Integer types are used to store whole numbers without decimals. Kotlin has four types of integers based on the range of numbers they can hold:

  • Byte: Stores small numbers from -128 to 127.
  • Short: Stores numbers from -32,768 to 32,767.
  • Int: Stores numbers from -2,147,483,648 to 2,147,483,647.
  • Long: Stores very large numbers, from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

Example:


fun main() {
    val age: Int = 25
    val distance: Long = 15000000000L  // Long type requires 'L' suffix
    val temperature: Byte = 100
}

2. Floating-Point Types

Definition: Floating-point types store numbers with decimals. Kotlin has two types of floating-point numbers:

  • Float: Stores smaller decimal numbers (precision up to 6-7 digits).
  • Double: Stores larger decimal numbers (precision up to 15-16 digits).

Example:


fun main() {
    val price: Float = 19.99F  // Float requires 'F' suffix
    val pi: Double = 3.141592653589793
}

3. Character Type

Definition: The Char type is used to store a single character, such as a letter, number, or symbol. Characters are enclosed in single quotes.

Example:


fun main() {
    val grade: Char = 'A'
    val symbol: Char = '#'
}

4. Boolean Type

Definition: The Boolean type is used to store either true or false. This is helpful when making decisions in the program (such as whether a condition is true or false).

Example:


fun main() {
    val isKotlinFun: Boolean = true
    val isRaining: Boolean = false
}

5. String Type

Definition: The String type is used to store sequences of characters, like words or sentences. Strings are enclosed in double quotes.

Example:


fun main() {
    val name: String = "John"
    val greeting: String = "Hello, Kotlin!"
}

6. Arrays

Definition: Arrays are used to store multiple values of the same type in a single variable. Each element in an array has an index, starting from 0.

Example:


fun main() {
    val numbers: Array = arrayOf(1, 2, 3, 4, 5)
    println(numbers[0])  // Output: 1
    println(numbers[2])  // Output: 3
}

Summary of Data Types

  • Integers: Byte, Short, Int, Long – Used to store whole numbers.
  • Floating-Point: Float, Double – Used to store decimal numbers.
  • Character: Char – Stores a single character.
  • Boolean: Boolean – Stores true or false.
  • String: String – Stores text or sequences of characters.
  • Array: Array – Stores multiple values of the same type.


MORE

Post a Comment

0Comments

Post a Comment (0)