Kotlin Data Type

In this post we are learning about basic data types of kotlin programming. First understand what is an data. In computer science data is an information there are need memory to store on information and there. And location of stored memory is called address. data type is specified the formatted categories of this data.

Data type is describing space of value and role of value. There is following data type is support in kotlin programming

Number

Number is an decimal and floating point value. programmer are capable to perform all type of mathematical operation on number data type. number is divided into 6 different parts.

Number Type Bits Bytes Range
Byte 8 1 -128 to 127
Short 16 2 -32768 to 32767
Int 32 4 -(2^31) to (2^31)-1
Float 32 4 -(2^31) to (2^31)-1
Long 64 8 -(2^63) to (2^63)-1
Double 64 8 -(2^63) to (2^63)-1

Int : Int (integer) are used to create a integer variable which are capable to store the values of signed 32 bit integers.

//sizeCapacity is an integer variable 
//that can be stored 32 bit signed integer value  
var sizeCapacity:Int =1000 //set integer value

Float : This type of variable is used to store Single-precision floating-point format of 32 bit.

//salary is an float variable 
var salary:Float =100000.80f //set float value

Double : This is similar to float but this are capable to held 64 bit Single-precision floating-point value.

var population:Double =100000000.80 //set value

Other data type of number is form of whole numbers. and their are used to held integers values. size are given in table. and here include basic example.

fun main(args: Array<String>) {
    
   var byteData:Byte=100 
   var decimalNumber:Int =1000
   var population:Long=89489259239
   var age:Short=21
   var floatData:Float=123.33f 
   var slaray:Double=500000.34  
   var binaryData: Int =0b10101 //binary literal
   println("var byteData  :"+byteData+ " Type : "+byteData.javaClass.kotlin) 
   println("var DecimalNumber :"+decimalNumber +" Type : "+decimalNumber.javaClass.kotlin) 
   println("var population  :"+population+ " Type : "+population.javaClass.kotlin)
   println("var age  :"+age+ " Type : "+age.javaClass.kotlin)
   println("var floatData  :"+floatData+ " Type : "+floatData.javaClass.kotlin)
   println("var slaray  :"+slaray+ " Type : "+slaray.javaClass.kotlin)
   println("var binaryData  :"+binaryData+ " Type : "+binaryData.javaClass.kotlin)
}

Output

var byteData  :100 Type : class kotlin.Byte
var DecimalNumber :1000 Type : class kotlin.Int
var population  :89489259239 Type : class kotlin.Long
var age  :21 Type : class kotlin.Short
var floatData  :123.33 Type : class kotlin.Float
var slaray  :500000.34 Type : class kotlin.Double
var binaryData  :21 Type : class kotlin.Int

kotlin is provide the underscore literals to decorate numbers. for example

fun main(args: Array<String>){
  var price = 1_2_000_000 //Underscores literals
  var amount = 50_000.60
  println("$price")  
  println("$amount")  
}
12000000
50000.6

when programmer are confused. like which type of number variable will store. then so we can define Number type of variable.

fun main(args: Array<String>) {
  
    var value:Number =10 //assign integer value
    println("Int value $value")
    
    value=100.23 //update double value
    println("Double value $value")
    
    value=10000L //assign long value
    println("Long value $value")
    
    value=10.90f //assign float value
    println("Float value $value")
}

Output

Int value 10
Double value 100.23
Long value 10000
Float value 10.9

Characters

Characters variable is capable to store single alphabets values. that is define of within the single quote. for example.

fun main(args: Array<String>) {
     
    var alphabets:Char ='a' 
    println("alphabets : $alphabets")
    
    alphabets='A' 
    println("alphabets : $alphabets")
}

Output

alphabets : a
alphabets : A

Booleans

Boolean value is contains True and False value. This is capable to perform logical operation in conditional statement.

fun main(args: Array<String>) {
 
   val status:Boolean=true

   if(status){
      println("You are ready to learn kotlin programming")
   }else{
      println("Oops you are not ready to learn kotlin")
   }
   
}

Output

You are ready to learn kotlin programming

Strings

String is sequence of single and multiple characters. string text should be defined within of double quotes. for example.

fun main(args: Array<String>){
  //defining of string variable and assign of text value
  var textInfo:String="Kotlin Programing"
  println("$textInfo")
}
Kotlin Programing

string text is similar to char array so there is possible to access string text by index value. See this example.

fun main(args: Array<String>){
  //defining of string variable and assign of text value
  var textInfo:String="Kotlin Programing"
  //access tring char by index
  println("${textInfo[0]}")
  println("${textInfo[7]}")
}

Output

K
P

String interpolation : $ dollar symbol is used to display value of variables within the string literals.

fun main(args: Array<String>){
  val number = 100
  //combine string text and variable value  
  val textValue = "number = $number"
  println(textValue)
}

Output

number = 100

Here given few basic examples of string operation.

Find length of string : kotlin is providing length to get the size of string.

fun main(args: Array<String>){
  //given text
  var textInfo:String="Kotlin Programing"
  val length=textInfo.length
  println("Size of [$textInfo] is : $length")
}

Output

Size of [Kotlin Programing] is : 17

compare two strings: Some case we are need to compare value to two strings. kotlin provide equals() function. this function are return boolean result.

fun main(args: Array<String>){
  //compaire two string values   
  var textData1:String="Kotlin code"
  var textData2:String="Kotlin code"
  //compare two string values
  println(textData1==textData2) //check logical
  println(textData1.equals(textData2)) //using of function


  textData1="Kotlin codes"
  println(textData1==textData2) //false
  println(textData1.equals(textData2)) //false
}

Output

true
true
false
false

Iterating string element

fun main(args: Array<String>){
 val text = "Kotlin"
   //iterating string element
    for (char in text){
        println(char)
    }

}

Output

K
o
t
l
i
n

Using while loop

fun main(args: Array<String>){
 val text = "Kotlin Text"
 var index = 0
 //iterating string elements by using while loop
  while (index < text.count()){
      println(text[index]) //get index element
      index++
  } 
}

Output

K
o
t
l
i
n
 
T
e
x
t

Find difference of two strings : Previous example equals() function is checking given string is equals or not. kotlin is provide compareTo() function to check given string difference.

fun main(args: Array<String>){
  //compaire two string values   
  var textData1:String="Kotlin code 12345" //Spaces is count
  var textData2:String="Kotlin code"
  
  println(textData1.compareTo(textData2)) //6
}

Output

6

Defining multi line Text: Multiline text can be defined inside the triple double quotes("""). for example.

fun main(args: Array<String>){
   //defining of multi line text
    var myText:String="""
   This is portion of long text we 
   are define bunch of 
   line text here
   """
   println("$myText")
}

Output

 This is portion of long text we 
   are define bunch of 
   line text here

Check sequence is exist or not: string is sequence of alphabets. so we can check given string pair is exist or not. kotlin is provide contains() function to do this task.

fun main(args: Array<String>){
   //defining of multi line text
    var myText:String="Learning about kotlin programming"

   println("${myText.contains("kotlin")}") //true
   println("${myText.contains("code")}") //false
}

Output

true
false

Concatenation: Using of plus operator there is combine two string in kotlin programming. see this example.

fun main(args: Array<String>){
   //defining of multi line text
    var firstText:String="Simple Text"
    var secondText:String="Special Text"
    //concatenation of strings
    var combine:String=firstText+" And "+secondText;
    println("$firstText")
    println("$secondText")
    println("$combine")

}

Output

Simple Text
Special Text
Simple Text And Special Text

kotlin check variable and object type

fun main(args: Array<String>) {
  var number:Int=100
  //get the class name of variable and object
  println(number.javaClass.name) 
  println(number.javaClass.kotlin)
  println(number.javaClass.kotlin.qualifiedName)
}

Output

int
class kotlin.Int
kotlin.Int


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved