Data types in kotlin
package com.example.my_kotlin_practice
fun main(args: Array<String>)
{
// Kotlin built in data type can be categorized as follows:
//
// Number
// Character
// String
// Boolean
// Array
var a : Int = 10000
var d : Double = 100.00
var f : Float = 100.00f
var l : Long = 1000000004
var s : Short = 10
var 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")
// Kotlin 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, like 'A' or '1'.
var letter : Char = 'A'
println("Character value is : $letter")
// Kotlin 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, \n in the following
// statement is a valid character, and it is called a new line character
println("\n") //prints a new line character
println('\$') //prints a dollar $ character
println("\\") //prints a backslash \ character
// The following escape sequences are supported in Kotlin: \t, \b, \n, \r, \', \", \\ and \$.
// String Data Type
var escapedString : String = "I am escaped String!\n"
var rawString : String = """This is going to be a multi-line string and will
not have any escape sequence
"""
print(escapedString)
println(rawString)
var escapedString2 : String = "I am escaped String!\n"
var rawString2 : String = """This is going to be a multi-line string and will
not have any escape sequence
""".trimMargin()
print(escapedString2)
println(rawString2)
// Boolean Data Type
var A : Boolean = true //defining a variable with true value
var B : Boolean = false //defining a variable with false value
println("Value of variable A is : $A")
println("Value of variable B is : $B")
// Boolean has a nullable counterpart Boolean? that can store a null value as below:
var boolNull: Boolean? = null
println("Value is : $boolNull")
// Array Data Type
// Kotlin 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.
var numbers : IntArray = intArrayOf(1,2,3,4,5)
println("Value at 3rd position : " + numbers[2])
// Data Type Conversion
// To convert a numeric data type to another type, Kotlin provides a set of functions:
//
// toByte()
// toShort()
// toInt()
// toLong()
// toFloat()
// toDouble()
// toChar()
var x : Int = 100
// var y : Long = x //Not valid assignment
var y : Long = x.toLong()
println(y)
}
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 Character value is : A $ \ I am escaped String! This is going to be a multi-line string and will not have any escape sequence I am escaped String! This is going to be a multi-line string and will not have any escape sequence Value of variable A is : true Value of variable B is : false Value is : null Value at 3rd position : 3 100 Process finished with exit code 0
Comments
Post a Comment