2. Generic Definition of Kotlin
● Kotlin is a general purpose, free, open source, statically typed
“pragmatic” programming language initially designed for the
JVM (Java Virtual Machine) and Android, and combines
object-oriented and functional programming features.
● It is focused on interoperability, safety, clarity, and tooling
support.
What is Kotlin
3. Kotlin and Java
● At first glance, Kotlin looks like a more concise and
streamlined version of Java.
● The most important reason for introducing Kotlin to Android
advancement was to decrease the number of lines of code
and make development more convenient.
● Everything that can be done utilizing Java can be done
utilizing Kotlin for Android development.
Why Kotlin ?
7. What is val and var?
In general, it is recommended to use ‘val’ whenever possible,
as it helps to enforce immutability and leads to cleaner, more
maintainable code. However, there may be cases where
mutability is necessary, in which case var can be used.
10. The when clause in Kotlin is a type of expression that works
similarly to a switch statement in other programming languages.
It evaluates a given expression and matches it with the branches
(cases) provided in the when clause.
The When Clause
11. In Android development, the when clause can be used to handle
different cases in a switch-like statement for example, to choose
different actions to perform based on the state of an object, the
user's inputs, or the device's screen orientation. Additionally, the
when clause can also be used in combination with the view model
to perform specific actions for a view based on changes in the
view's state.
The When Clause
14. What are lambda functions?
Lambdas expression are function literals means these
functions are not declared but passed immediately as an
expression.
val sum1 = { a: Int, b: Int -> a + b }
or
val sum2:(Int,Int)-> Int = { a , b -> a + b}
Lambda function
15. Kotlin Collections
What is a collection?
● A collection is a group of related items.
● It is simply an object that groups multiple elements
into a single unit.
● Kotlin supports different kinds of collections
including arrays, lists, mutable lists etc.
GT
Mythpa
13.1M
16. Arrays
What are Arrays?
Arrays are used to store multiple values in a single
variable, instead of creating separate variables for each
value.
To create an array, use the arrayOf() function, and place
the values in a comma-separated list inside it:
val cars = arrayOf("Volvo", "BMW", "Ford", "Mazda")
18. Methods in Array
1. size
Gives the size of the array.
array.size
2. get()
Gives the element at the specified position.
array.get(element)
19. 3. set()
Changes the element at the specified position.
array.set()
4. indices
returns an IntRange object with valid indices for this list.
array.indices
20. 5. forEach
forEach is used perform the given action on each item of
the array. Inside the code block of forEach, the item
could be referenced as it.
the array.forEach {
//statement(s)
}
21. Lists
What are Lists?
A list is a collection of items with a
specific order. It is declared using
listOf() syntax. It’s size is fixed. List is
same as array except that in list
elements cannot be modified.
val numbers = listOf(1, 2, 3, 4, 5, 6)
22. Methods in List
1. forEach()
foreach is used perform the given action on each item of
the list. Inside the code block of forEach, the item could
be referenced as it.
the List.forEach {
//statement(s)
}
23. 2. size
gives the size of the list.
list.size
3. contains()
returns true if element is found in the list, else false.
list.contains(element)
24. 4. first()
used to get the first element of the List.
list.first()
3. last()
used to get the last element of the List.
list.last()
25. 5. get()
returns the element at the specified index in this list.
list.get(index)
6. indices
returns an IntRange object with valid indices for this list.
list.indices
26. 7. indexOf()
finds and returns the index of first occurrence of the
element in the list.
list.indexOf(element)
27. Mutable lists
What are mutable lists?
MutableList class is used to create
mutable lists in which the
elements can be added or
removed.
fun <T> mutableListOf( vararg
elements: T): MutableList <T>
28. Methods in list:
1. add()
It adds elements to the list.
val numbers = mutableListOf(1, 2, 3, 4, 5, 6)
numbers.add(7)
29. 2. size
gives the size of the list.
numbers.size
3. contains()
returns true if element is found in the list, else false.
numbers.contains(element)
30. 4. first()
used to get the first element of the List.
numbers.first()
3. last()
used to get the last element of the List.
numbers.last()
31. 5. get()
returns the element at the specified index in this list.
numbers.get(index)
6. indices
returns an IntRange object with valid indices for this list.
numbers.indices
32. 7. indexOf()
finds and returns the index of first occurrence of the
element in the list.
numbers.indexOf(element)
8. elementAt()
Returns the element at the specified index.
numbers.elementAt(index)
33. 9. remove()
Removes the specified element from the list.
numbers.remove(element)
10. removeAt()
Removes the element from the list at specified
position.
numbers.removeAt(element)
39. ● No constructors are allowed (init blocks are allowed)
● Follows singleton pattern
● Can inherit a class/interface
● Only one instance of the object is created
Properties of an object
41. Inheritance
What is Inheritance?
● Mechanism in which one
object acquires all the
properties and behaviors of
a parent object.
● Inheritance represents the
IS-A relationship between
objects.
46. Data classes in Kotlin are special
classes specifically designed to
make data storage easy and
convenient. They provide a concise
way of declaring classes without the
need for writing any getters, setters,
equals, hashCode, or toString
methods.
Data classes
What are they ?
51. Java and Kotlin both are object-oriented programming
languages. But both are used for different purposes.
Kotlin is used to develop android applications while Java
is mainly used for developing enterprise applications.
Kotlin is used when we want to develop an android
application or want to do server-side programming. We
should go with Java if we want to develop an enterprise
application.
Java and Kotlin
Comparison between Java and Kotlin