Exploring Kotlin with Me #3

Arun Pandian M
3 min readJun 30, 2018

--

Data Types

In Programming Language to operate the variables, important to Know about DataTypes. In Kotlin, all data type are an object, that means we can call Member function and properties. Data types categorized as the set of relative values and used to define the operation.

Courtesy : Photo by Simon Rae on Unsplash

Available datatypes in Kotlin are

  1. Numbers
  2. Characters
  3. Strings
  4. Booleans
  5. Arrays

Let’s start to know about above Data types.

Numbers

For Numbers, kotlin has 6 built-in datatypes.Numbers in Kotlin are similar to Java.

  • Byte 8 bit
  • Short 16 bit
  • Int 32 bit
  • Long 64 bit
  • Float 32 bit
  • Double 64 bit

Byte

  • It can have values from -127 to 127
  • Mostly to reduce the memory size, it is used instead of an integer.
val byteVal: Byte = 22
val errorValue: Byte = 125//compile error

Short

  • The Short data type can hold values from -32768 to 32767
  • When you use number values within the range of -32768 to 32767 means we can use this type for reduce memory
val shortVal: Short = -22395

Int

  • The Int data type can hold values from -2^31 to (2^31)-1
var intValue: Int = 1039094

Long

  • The Int data type can hold values from -2^63 to (2^63)-1
  • For very long range of values which not in the range of Int, then Long is used
  • Capital letter L to denote that the variable is of type Long
val sunDistance: Long =149600000000 
var longValue: Long =10L

Double

  • 64 bit double-precision floating point value.
val doubleVal = 325.49

Float

  • 32 bit single-precision floating point value.
  • Capital letter L to denote that the variable is of type Float
val floatValue = 19.5F

Note: We can also use underscore in numeric values to make them more readable like following

val oneMillion = 1_000_000
println("a one million $oneMillion")//prints 'a one million 1000000'

Characters

Characters are represented using the type Char. Unlike Java, Char types cannot be treated as numbers. They are declared using single quotes like this

val letterChar:Char = 'A'

var digitChar:Char ='2'

Strings

In Kotlin Strings are represented by String class. Strings are immutable.

var stringValue :String = "Hello friends"

Elements of a string are characters that can be accessed by the indexing operation: s[i]. A string can be iterated over with a for-loop like following:

var stringValue :String = "Hello friends"


for (c in stringValue) {
println(c)
}

Just like other languages, special characters in Kotlin are escaped using a backslash. Some examples of escaped characters are — \n (newline), \t (tab), \r (carriage return), \b (backspace) etc.

In Kotlin, we can give the Raw String, but in this escaping Characters are not cared

var myMultilineRawString = """
Hello friends, \n new line won't create
"""

Boolean

  • Boolean has two possible values that true or false
var boolValue : true

Arrays

In Kotlin Arrays are represented by Arrays class.We can create an array in Kotlin either using the library function arrayOf() or using the Array() constructor.

arrayOf()

  • we can pass the punch of values to create the array using arrayof() function like following:
// using arrayOf() 
var numbers = arrayOf(1, 2, 3, 4, 5)
var names = arrayOf("Arun", "Ashok", "Bala", "Chandru")
//Array() Constructor
  • we can create mixed array list using a arrayOf() function.
var mixedArray = arrayOf(22, true,  "Arun", 'A')    
  • we can create the arraylist in purticular type like following
var numArray = arrayOf<Int>(1, 2, 3, 4)
  • We can create Primitive arrays in kotlin using intArrayOf(), booleanArrayOf(),doubleArrayOf(), charArrayOf etc like below
val charArrayValue = charArrayOf('A', 'P', 'M')
var intArrayValue = intArrayOf(22,33,44,55)
var booleanArrayValue = booleanArrayOf(true,false,true)

ArrayConstructer

following example for creating array using array constructer

5 is Array of index and it denotes the index.

{1,2,3,4,5} is the array for following example

val numbers2 = IntArray(5) { (it + 1)  }

Okay its weekend i don’t want to type much more. I hope you really liked and learnt something about Kotlin data types.

I’m off ! bye bye. I will come with type conversion and operators in Kotlin in my next post. If you have any feedbacks or doubts, please leave that as a comment.

And if you really liked this post and want to make me feel happy, don’t forget to 👏!

--

--

Arun Pandian M
Arun Pandian M

Written by Arun Pandian M

Senior Android developer at FundsIndia, A time investor to learn new things about Programming. Currently in a relationship with Green Bug(Android).

No responses yet