Kotlin Basic Data Types

By | October 28, 2023

Kotlin Basic Data Types  – Kotlin employs basic data types to represent fundamental kinds of values and variables. These data types are essential for various programming tasks.

Kotlin Basic Data Types With Examples

Here are the commonly used data types in Kotlin:

Numbers:

  • Byte: Represents 8-bit signed integers, which means it can store whole numbers in the range from -128 to 127. It is typically used when memory conservation is a concern, or when you need to work with small integers
  • val smallNumber: Byte = 42
  • Short: Represents 16-bit signed integers, covering a broader range than Byte. It can hold whole numbers in the range from -32,768 to 32,767. Use Short when you need a larger integer range than Byte but still want to conserve memory compared to Int or Long.
  • val smallInteger: Short = 1000
  • Int: Represents 32-bit signed integers, Int is one of the most commonly used data types in Kotlin and represents 32-bit signed integers. It can hold whole numbers within a wide range, from approximately -2.1 billion to 2.1 billion.
    Int is suitable for most everyday integer calculations.
  • val integerNumber: Int = 12345
  • Long: Represents 64-bit signed integers. Long represents 64-bit signed integers, which provides an extensive range for whole numbers.
    It can store values ranging from approximately -9.2 quintillion to 9.2 quintillion.
    Use Long when you need to work with very large integer values that go beyond the range of Int.
  • val largeNumber: Long = 9876543210
  • Float: Represents 32-bit single-precision floating-point numbers.Float represents 32-bit single-precision floating-point numbers. It is used for representing decimal numbers with moderate precision.
    It is suitable for most cases where you need floating-point numbers, but high precision is not a requirement.
    Floating-point numbers can represent a wide range of values, but they have limited precision due to the 32-bit representation.
  • val floatValue: Float = 3.14159f
  • Double: Represents 64-bit double-precision floating-point numbers.
  • Double represents 64-bit double-precision floating-point numbers, which provide higher precision compared to Float.
    It is the default choice for most floating-point calculations and is suitable for applications requiring high precision, such as scientific calculations.
    Double can represent a vast range of decimal values with much greater accuracy than Float.
  • val doubleValue: Double = 3.141592653589793

Example:

val age: Int = 25
val price: Double = 19.99

2. Characters:

  • Char: Denotes a single 16-bit Unicode character.

Example:

val initial: Char = 'A'

3. Booleans:

  • Boolean: Represents either true or false.

Example:

val isStudent: Boolean = true

4. Strings:

  • String: Represents a sequence of characters.

Example:

val name: String = "John"

5. Arrays:

  • Array: Represents an array of elements. You can specify the element type, such as IntArray or StringArray.

Example:

val numbers: IntArray = intArrayOf(1, 2, 3, 4, 5)

6. Ranges:

  • Kotlin offers range types like IntRange and LongRange to denote a range of values.

Example:

val range: IntRange = 1..10

Null Safety:

  • Kotlin enables you to indicate whether a data type can hold null by appending ? to the type. For instance, String? signifies a variable that can be a non-null string or null.

Example:

val email: String? = null

These fundamental data types serve various programming needs about data in user programs. Selecting the appropriate data type is vital for effective memory management and type safety in your code.

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *