Unit 1
Unit 1
Unit 1 :
1. Introduction of Kotlin:
Kotlin is a modern, statically-typed programming language that runs on the Java Virtual Machine
(JVM) and is designed to be fully interoperable with Java. It is known for its concise syntax,
safety features, and modern approach to programming. Kotlin is used for Android app
development, backend development, and more.
Example:
fun main() {
println("Hello, Kotlin!")
}
To start using Kotlin, you need an Integrated Development Environment (IDE). IntelliJ IDEA is
one of the most popular IDEs for Kotlin development.
Once installed, you can use IntelliJ IDEA to write, compile, and run Kotlin programs.
4. Use of Kotlin:
REPL stands for Read-Eval-Print Loop, a tool that allows you to write and test Kotlin code
interactively. It evaluates expressions and prints results immediately. It’s useful for learning and
experimenting with Kotlin code.
To use REPL in IntelliJ IDEA: Open the Kotlin REPL window and start typing Kotlin
expressions.
Control flow statements in Kotlin help you control the flow of the program, like if-else
conditions and loops.
If-else:
val number = 10
if (number > 5) {
println("Greater than 5")
} else {
println("Less than or equal to 5")
}
val number = 2
when (number) {
1 -> println("One")
2 -> println("Two")
else -> println("Other")
}
for (i in 1..5) {
println(i)
}
var count = 1
while (count <= 5) {
println(count)
count++
}
Non-nullable variable:
Kotlin allows functions to have default values for parameters, and named arguments for clarity.
Default Arguments:
Named Arguments:
Example:
fun operateOnNumbers(a: Int, b: Int, operation: (Int, Int) -> Int): Int {
return operation(a, b)
}
A lambda expression is a concise way to define anonymous functions in Kotlin. Lambdas are
often used with higher-order functions like map, filter, etc.
Example:
val square = { x: Int -> x * x }
println(square(5)) // Outputs: 25
Object-Oriented Programming (OOP) is a programming paradigm that uses objects and classes.
In Kotlin, OOP is used to organize and structure code. It makes code more modular, reusable,
and easier to maintain. OOP principles are based on 4 main concepts: Encapsulation,
Abstraction, Inheritance, and Polymorphism.
Example: In Kotlin, classes are blueprints that define objects. Objects represent instances
of these classes.
Class: A class is a blueprint or template for creating objects. It defines properties and
methods that an object can have.
Object: An object is an instance of a class. When you create an object, it uses the class as
its blueprint.
Example:
13. Constructors:
A constructor is a special function in a class that is called when an object is created. It initializes
the object with values.
Visibility modifiers define the accessibility of classes, methods, and properties. Kotlin has the
following visibility modifiers:
Example:
class Car {
private val engine = "V8" // Only accessible within the Car class
fun start() {
println("Engine type: $engine")
}
}
Inheritance allows a class (child or subclass) to inherit properties and methods from another class
(parent or superclass). This promotes reusability.
Example:
open class Animal(val name: String) { // Parent class
fun speak() {
println("$name makes a sound")
}
}
16. Interfaces:
An interface defines a contract of methods that a class must implement. It’s similar to a class but
can’t have a body for the methods. A class can implement multiple interfaces.
Example:
interface Drivable {
fun drive()
}
A data class is a special class in Kotlin that automatically provides useful methods such as
equals(), hashCode(), and toString() for its properties. It is primarily used to store data.
Example:
Example:
object Singleton {
val name = "Unique Instance"
}
println(Singleton.name) // Accessing the singleton object
Enum: A special class that represents a group of constants. It’s useful when you have a
predefined set of values, like days of the week or seasons.
Example:
19. Pairs:
A Pair is a simple class that holds two values. It's often used when you want to return two
related values from a function.
Example:
20. Triples:
A Triple holds three values, similar to a Pair but with one additional value. It's useful when you
need to return three related values.
Example:
Collections are data structures that hold multiple elements. Kotlin has three main types of
collections:
List: Ordered collection of elements. It can be mutable or immutable.
o MutableList: Allows modification (add/remove elements).
o List: Immutable; can't be modified after creation.
Example:
Example: