Kotlin Fundamental


Introduction to Kotlin

Kotlin Fundamental

What is Kotlin?

  • Kotlin is a programming language.

  • You use it to build Android apps.

  • It runs on JVM (Java Virtual Machine).

  • It works well with Java.

Simple Meaning:
Kotlin helps you write code in a short and clean way.

Real-Life Example

  • Apps like Instagram and shopping apps use Kotlin for Android.

  • When you open a food delivery app, Kotlin may run behind it.

Why Learn Kotlin?

  • Easy to learn

  • Less code than Java

  • Safe from common errors

  • High demand in Android jobs

College Example

  • If you build a college attendance app, Kotlin helps you write less code and finish fast.

Exam Tip

👉 Definition: Kotlin is a modern programming language used mainly for Android app development.

Basic Syntax

How Kotlin Code Looks

fun main() {
    println("Hello World")
}
  • fun means function (a block of code that runs).

  • main() is starting point.

  • println() prints output.

Daily Example
Like pressing the start button on a washing machine.
main() is that start button.

Variables

Variables store data.

val name = "Sainy"
var age = 20
  • val → value cannot change.

  • var → value can change.

Shopping Example

  • Your date of birth = val (never changes)

  • Your cart items = var (you change it)

Exam Tip

👉 val = fixed
👉 var = changeable

Coding Conventions

Coding conventions mean writing code in clean style.

  • Use meaningful names.

  • Start function name with small letter.

  • Use proper spacing.

College Example
Writing neat notes helps teacher read easily.
Clean code helps others understand easily.

Basic Types

Data types tell what kind of data we store.

Type Meaning Example
Int Whole number 10
Double Decimal number 10.5
String Text "Hello"
Boolean True/False true

Mobile App Example

  • Price = Double

  • Username = String

  • Login success = Boolean

Packages

Package means folder that groups related files.

package com.college.app

Example
Like arranging books subject-wise in your bag.

Control Flow

Control flow controls program direction.

If-Else

if (marks > 40) {
   println("Pass")
}

College Example
If attendance > 75 → allowed in exam.

When (Like Switch)

when(day) {
   1 -> println("Monday")
}

Daily Example
If today is Sunday → Relax.

Loops

Repeat code.

for(i in 1..5) {
   println(i)
}

Example
Calling 5 students one by one.

Returns and Jumps

Return

Stops function and sends value back.

return 10

Example
You submit assignment and get marks back.

Break & Continue

  • break → stop loop

  • continue → skip current step

Example
Stop watching movie in middle = break
Skip one question in exam = continue

Classes and Objects

Class

Blueprint (design).

Object

Real item created from class.

class Student {
   var name = ""
}

Example
Class = Car design
Object = Actual car

Inheritance

Inheritance means child gets features of parent.

open class Animal
class Dog: Animal()

Example
Child gets parents’ properties.

Properties and Fields

Property = variable inside class.

var age = 20

Field = actual memory that stores value.

Example
Student class has property: roll number.

Interfaces

Interface gives rules.

interface Drive {
   fun start()
}

Example
College rule book. Students must follow.

Visibility Modifiers

Control access.

Modifier Meaning
public Everyone can use
private Only inside class
protected Family access
internal Same module

Example
Private diary = private
College notice board = public

Extensions

Add new function to existing class.

Example
Add new feature to WhatsApp without changing old code.

Data Classes

Used to store data only.

data class User(val name: String)

Auto gives:

  • toString

  • equals

  • copy

Example
Student record form.

Generics

Generics allow flexible data types.

class Box<T>

Example
Gift box that can store any item.

Nested Classes

Class inside another class.

Example
Department inside College.

Enum Classes

Fixed set of values.

enum class Day { MONDAY, TUESDAY }

Example
Days of week.

Objects

Used to create single instance.

object Database

Example
Principal of college (only one).

Delegation

Delegation means giving work to someone else.

Example
Class monitor collects assignments.

Delegated Properties

Property handled by another class.

Example:
Lazy loading (load when needed).

Shopping Example
App loads product details only when clicked.

Functions

Block of code that performs task.

fun add(a:Int,b:Int):Int {
   return a+b
}

Lambdas

Short function without name.

val sum = {a:Int,b:Int -> a+b}

Example
Quick note instead of full letter.

Higher-Order Functions

Function that takes another function.

Example
Teacher giving task to class monitor.

Inline Functions

Reduce memory usage.

Used when function repeats many times.

Scope Functions

Help manage object easily.

Important ones:

  • let

  • apply

  • run

  • with

  • also

Example
While filling form, you stay in same form area and fill all fields.

Collections

Store multiple items.

Type Example
List Ordered items
Set No duplicate
Map Key-Value

Example
Shopping cart = List
Unique roll numbers = Set
Student ID → Name = Map

Ranges

Range means between values.

1..10

Example
Roll numbers 1 to 60.

Type Checks and Casts

Check type using is.

if (name is String)

Example
Check if person is student before giving ID card.

This Expression

this refers to current object.

Example
You pointing to yourself in class.

Equality

  • == checks value

  • === checks memory location

Example
Two students have same marks (==)
Same student object (===)

Operator Overloading

Change meaning of operators.

Example:
+ can add objects.

Null Safety

Kotlin prevents null errors.

var name: String? = null

Use ? for nullable.

Example
Empty seat in class = null.

Exam Tip

👉 Kotlin avoids Null Pointer Exception.

Exceptions

Handle errors.

try {
}
catch(e:Exception) {
}

Example
If ATM fails, show error message.

Annotations

Extra information for compiler.

Example:
@Override

Like teacher writing remark on assignment.

Reflection

Reflection checks class details at runtime.

Simple meaning:
Program checks itself.

Important Exam Questions

Short Questions

  1. What is Kotlin?

  2. Difference between val and var?

  3. What is null safety?

  4. What is data class?

  5. What is lambda?

Long Questions

  1. Explain classes and inheritance.

  2. Explain collections with examples.

  3. Explain control flow in Kotlin.

  4. Explain visibility modifiers.

  5. Explain higher-order functions.

Quick Revision Table

Topic Key Point
val Cannot change
var Can change
Class Blueprint
Object Instance
Enum Fixed values
List Ordered
Set Unique
Map Key-Value
? Nullable

Final Summary

  • Kotlin is modern and easy.

  • It reduces code.

  • It prevents null errors.

  • It supports object-oriented concepts.

  • It supports functional style (lambdas).

  • It works mainly for Android apps.

Remember This

val fixed, var change
== value check
? allows null
✅ Data class stores data
✅ Collection stores many items

These notes help you revise fast and score well in exams.