Sri Krishna College of Engineering and Technology
Module 1-Introduction
1.2 Kotlin Data Types &
Operators 1
Agenda
Operators
Data Types
2
Recap of Var vs Val
Predict the output for the following code snippets
fun main()
{
var 1=“Hi students”
println(1)
}
3
Kotlin - Data types
4
Kotlin - Data types
In Kotlin, the type of a variable is decided by its value:
5
Kotlin - Data types
6
Kotlin - Data types
Floating Point
Numbers String Boolean
Numbers
• Byte - 8 bit [- • Float - 32 bit • char
• Boolean
128 to 127] • Double - 64 • String
• Short - 16 bit bit
[-32768 to
32767]
• Int - 32 bit [-
231 to 231-1]
• Long - 64 bit
[-263 to 263-
1]
7
Operators in Koltin
Arithmetic operator
Relational operator
Unary operator
Bitwise operator
Logical operator
Assignment operator
8
1. Arithmetic Operators
9
Example program
fun main (args : Array <String>) fun main (args : Array <String>)
{ {
var num1 = 64 val fname = “Sundar"
var num2 = 32 val lname = “Pichai"
val answer : double val full_name = fname + " " + lname
answer = num1 +num2 println (full_name)
println ("sum = $answer") }
answer = num1 - num2
println ("diff = $answer")
answer =num1 * num2
println ("mult = $answer")
answer = num1 / num2
println ("div = $answer")
answer = num1 % num2
println ("mod = $answer")
}
10
2. Assignment Operators
11
3. Unary Operators
12
4. Comparison Operators
13
5. Equality and Non-equality Operators
14
6. Logical Operators
15
7. In Operator
16
7. In Operator – Example Code
fun main (args : Array <String>)
{
val array = intArrayOf(10, 20, 30 ,40)
If (20 in array)
{ println ("yes 20 is present in array")
}
else
{ println ("no 20 is not present in array")
}
} 17
8. Range Operator
To create a range of values.
Very useful when working with loops.
It is unnecessary to define every value if it is sequential
It is better to use a shortcut and define the range specifying the
lowest and highest value.
18
8. Range Operator
19
8. Range Operator – Example Code
fun main (args : Array <String>)
{
for (i in 1..10)
{
println ("value of i is $i")
}
}
20
9. Indexed Access Operator
21
9. Indexed Access Operator – Example Code
fun main (args : Array <String>)
{
val array = intArrayOf(10, 20, 30, 40, 50)
var value = array[1]
println("value at index 1 is $value“)
array[1] = 90
println ("recent value at index 1 is $array[1]")
}
22
10. Bitwise Operators
Like other programming
languages, e.g. C, C++,
Java, Kotlin does not have
any bitwise operators.
It has various functions
that work for bitwise
operations.
23
10. Bitwise Operators – Example Code
fun main (args : Array <String>)
{
var a = 12
var b = 10
val result1 : Int
val result2 : Int
result1 = a and b
result2 = a or b
println ("final result of and operation is $result1")
println("final result of or operation is $result2“)
} 24
Thank You!!!
25