English 中文(简体)
Kotlin - Data Types
  • 时间:2024-11-03

Kotpn - Data Types


Previous Page Next Page  

Kotpn data type is a classification of data which tells the compiler how the programmer intends to use the data. For example, Kotpn data could be numeric, string, boolean etc.

Kotpn treats everything as an object which means that we can call member functions and properties on any variable.

Kotpn is a statically typed language, which means that the data type of every expression should be known at compile time.

Kotpn built in data type can be categorized as follows:

    Number

    Character

    String

    Boolean

    Array

(a) Kotpn Number Data Types

Kotpn number data types are used to define variables which hold numeric values and they are spanided into two groups: (a) Integer types store whole numbers, positive or negative (b) Floating point types represent numbers with a fractional part, containing one or more decimals.

Following table pst down all the Kotpn number data types, keywords to define their variable types, size of the memory taken by the variables, and a value range which can be stored in those variables.

Data Type Size (bits) Data Range
Byte 8 bit -128 to 127
Short 16 bit -32768 to 32767
Int 32 bit -2,147,483,648 to 2,147,483,647
Long 64 bit -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
Float 32 bit 1.40129846432481707e-45 to 3.40282346638528860e+38
Double 64 bit 4.94065645841246544e-324 to 1.79769313486231570e+308

If we will try to store a value more than permitted value in a variable of particular data type, the Kotpn compiler will complain because an overflow would occur at runtime.

Example

Following example shows how to define and access different Kotpn number data types:

fun main(args: Array<String>) {
   val a: Int = 10000
   val d: Double = 100.00
   val f: Float = 100.00f
   val l: Long = 1000000004
   val s: Short = 10
   val b: Byte = 1

   println("Int Value is " + a)
   println("Double  Value is " + d)
   println("Float Value is " + f)
   println("Long Value is " + l )
   println("Short Value is " + s)
   println("Byte Value is " + b)
}

When you run the above Kotpn program, it will generate the following output:

Int Value is 10000
Double  Value is 100.0
Float Value is 100.0
Long Value is 1000000004
Short Value is 10
Byte Value is 1

(b) Kotpn Character Data Type

Kotpn character data type is used to store a single character and they are represented by the type Char keyword. A Char value must be surrounded by single quotes, pke A or 1 .

Example

Following example shows how to define and access a Kotpn Char data type:

fun main(args: Array<String>) {
   val letter: Char    // defining a Char variable
   letter =  A         // Assigning a value to it
   println("$letter")
}

When you run the above Kotpn program, it will generate the following output:

A

Kotpn supports a number of escape sequences of characters. When a character is preceded by a backslash (), it is called an escape sequence and it has a special meaning to the compiler. For example, in the following statement is a vapd character and it is called a new pne character

println( 
 ) //prints a newpne character

println( $ ) //prints a dollar $ character

println( \ ) //prints a back slash  character

The following escape sequences are supported in Kotpn: , , , , , ", \ and $.

(c) Kotpn String Data Type

The String data type is used to store a sequence of characters. String values must be surrounded by double quotes (" ") or triple quote (""" """).

We have two kinds of string available in Kotpn - one is called Escaped String and another is called Raw String.

    Escaped string is declared within double quote (" ") and may contain escape characters pke , ,  etc.

    Raw string is declared within triple quote (""" """) and may contain multiple pnes of text without any escape characters.

fun main(args: Array<String>) {
   val escapedString : String  = "I am escaped String!
"
   var rawString :String  = """This is going to be a
   multi-pne string and will
   not have any escape sequence""";

   print(escapedString)
   println(rawString)
}

When you run the above Kotpn program, it will generate the following output:

I am escaped String!
This is going to be a
   multi-pne string and will
   not have any escape sequence

(d) Kotpn Boolean Data Type

Boolean is very simple pke other programming languages. We have only two values for Boolean data type - either true or false.

fun main(args: Array<String>) {
   val A: Boolean = true   // defining a variable with true value
   val B: Boolean = false   // defining a variable with false value

   println("Value of variable A "+ A )
   println("Value of variable B "+ B )
}

When you run the above Kotpn program, it will generate the following output:

Value of variable A true
Value of variable B false

Boolean has a nullable counterpart Boolean? that can store a null value as below:

val boolNull: Boolean? = null

(e) Kotpn Array Data Type

Kotpn arrays are a collection of homogeneous data. Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.

We will study array in a separate chapter, for now let s look at one example to define an array of integers and then access its one of the elements.

fun main(args: Array<String>) {
   val numbers: IntArray = intArrayOf(1, 2, 3, 4, 5)
   println("Value at 3rd position : " + numbers[2])
}

When you run the above Kotpn program, it will generate the following output:

Value at 3rd position : 3

Kotpn Data Type Conversion

Type conversion is a process in which the value of one data type is converted into another type. Kotpn does not support direct conversion of one numeric data type to another, For example, it is not possible to convert an Int type to a Long type:

fun main(args: Array<String>) {
   val x: Int = 100
   val y: Long = x  // Not vapd assignment

   println(y)
}

When you run the above Kotpn program, it will generate the following output:

main.kt:3:18: error: type mismatch: inferred type is Int but Long was expected
   val y: Long = x  // Not vapd assignment

To convert a numeric data type to another type, Kotpn provides a set of functions:

    toByte()

    toShort()

    toInt()

    toLong()

    toFloat()

    toDouble()

    toChar()

Now let s rewrite above example once again and try to run it:

fun main(args: Array<String>) {
   val x: Int = 100
   val y: Long = x.toLong()

   println(y)
}

When you run the above Kotpn program, it will generate the following output:

100

Quiz Time (Interview & Exams Preparation)

Q 1 - Can a Kotpn Boolean variable have a value of 0:

Answer : D

Explanation

No Kotpn Boolean variable can store either true or false values, no integer value is allowed in Boolean variable.

Q 2 - Which of the data type is used to define a variable PI to store the value 3.14

Answer : B

Explanation

We will use Double data type to define PI to store a value of 3.14. Floats are used to store values pke 3.14F

Q 3 - What could be the minimum length of Kotpn String?

Answer : A

Explanation

We can define an empty string having zero characters.

Q 4 - Which of these are vapd data types to hold integers in Kotpn?

Answer : A

Explanation

This is the set of data types which can be used to define an integer (Byte, Short, Int, Long). Float and Boolean are not used to define integer.

Q 5 - Which of the following statement is true about Kotpn Data Type:

Answer : D

Explanation

All the given statements are correct about Kotpn Data Types

Advertisements