KOTLIN TUTORIAL A Beginner - Friendly Guide to Kotlin Programming Prepared by Rachit Joshi 1. Introduction Kotlin is a modern, statically typed, general - purpose programming language. It is concise, expressive, open - source, and designed to work smoothly with Java. Kotlin is widely used for Android development and is also suitable for server - side applications, desktop software, and multiplatform projects. Kotlin source files commonly use the .kt extension. 2. History of Kotlin Kotlin was created by JetBrains. Development began around 2010, and Kotlin 1.0 was officially released in February 2016. The language was named after Kotlin Island near St. Petersburg, Russia. 3. Features of Kotlin • Concise syntax reduces repetitive code and boilerplate. • Null safety distinguishes nullable and non - nullable types to help prevent null - reference errors. • Java interoperability allows Kotlin and Java code to call each other and share JVM libraries. • Smart casts let the compiler use a more specific type after suitable checks. • Extension functions add functionality to existing types without changing their source code. • Data classes generate useful methods for classes primarily used to hold data. • Coroutines support structured asynchronous programming. • Strong IDE and build - tool support assists coding, testing, and debugging. 4. Setting Up the Kotlin Environment Kotlin can be used with IntelliJ IDEA, Android Studio, or another editor with Kotlin support. For command - line development, install a compatible JDK and Kotlin compiler. Android projects are commonly created and managed in Android Studio using Gradle. 5. First Kotlin Program The main() function is the entry point for a basic Kotlin console program. fun main() { println("Hello, Kotlin!") } The fun keyword declares a function. println() displays text followed by a line break. 6. Variables: val and var Use val for a read - only reference and var for a variable that can be reassigned. Prefer val when reassignment is unnecessary. val language = "Kotlin" var year = 2026 year = 2027 println("$language in $year") Keyword Meaning Example val Read - only reference val name = "Asha" var Reassignable variable var score = 10 7. Kotlin Data Types Kotlin includes numeric, character, Boolean, string, and array types. The compiler can often infer a variable's type from its initial value. Category Types Example Integers Byte, Short, Int, Long val count: Int = 25 Floating point Float, Double val price: Double = 19.5 Character Char val grade: Char = 'A' Boolean Boolean val ready = true Text String val city: String = "Noida" 8. Type Conversion Use explicit conversion functions when changing numeric types, such as toInt(), toLong(), toDouble(), and toFloat(). val number: Int = 42 val decimal: Double = number.toDouble() 9. Operators Group Examples Purpose Arithmetic + - * / % Calculations Comparison == != < > <= >= Compare values Logical && || ! Combine Boolean conditions Assignment = += - = *= /= Assign or update values Range .. downTo step Create ranges Membership in !in Test membership 10. Input and Output print() displays output without automatically adding a line break. println() adds a line break. readLine() reads a line of text in simple console programs. fun main() { print("Enter your name: ") val name = readLine() println("Welcome, $name") } 11. Comments Comments document code and are ignored by the compiler. Kotlin supports single - line, multi - line, and documentation comments. // Single - line comment /* Multi - line comment */ /** Documentation comment */ 12. Control Flow if Expression Kotlin's if can be used as a statement or as an expression that returns a value. val age = 20 val category = if (age >= 18) "Adult" else "Minor" when Expression when is a flexible alternative to a long chain of if - else conditions. val day = 3 val name = when (day) { 1 - > "Monday" 2 - > "Tuesday" 3 - > "Wednesday" else - > "Other day" } Loops Kotlin supports for, while, and do - while loops. A for loop iterates over ranges or collections; while repeats while its condition is true; do - while checks the condition after executing the body. for (number in 1..5) { println(number) } var count = 1 while (count <= 3) { println(count); count++ } 13. Functions Functions group reusable instructions. Parameters define inputs, and a return type describes the value returned. fun add(a: Int, b: Int): Int { return a + b } fun main() { println(add(5, 3)) } Kotlin also supports single - expression functions, default arguments, named arguments, and local functions. fun greet(name: String = "Guest") = "Hello, $name" println(greet()) println(greet(name = "Mira")) 14. Arrays and Strings An array stores a fixed - size sequence of elements. Use arrayOf() to create a general array. Strings are immutable sequences of characters and support string templates. val numbers = arrayOf(10, 20, 30) println(numbers[0]) val firstName = "Sam" println("Hello, $firstName") 15. Null Safety Kotlin distinguishes nullable and non - nullable references. String cannot normally hold null, while String? can. Safe calls (?.), the Elvis operator (?:), and safe casts (as?) help handle nullable values. val name: String? = null val length = name?.length ?: 0 println(length) The not - null assertion operator (!!) can throw an exception if the value is null, so use it only when non - nullness is guaranteed. 16. Collections Collections group multiple values. Kotlin provides read - only and mutable collection interfaces for lists, sets, and maps. Collection Creation Description List listOf(1, 2, 3) Ordered; duplicates allowed MutableList mutableListOf(1, 2, 3) List that can be changed Set setOf("A", "B") Unique elements Map mapOf("id" to 1) Key - value pairs MutableMap mutableMapOf("id" to 1) Map that can be changed 17. Object - Oriented Programming Kotlin supports classes, objects, constructors, inheritance, interfaces, abstract classes, and visibility modifiers. A class describes the structure and behaviour of objects. class Student(val name: String, var marks: Int) { fun display() { println("$name: $marks") } } fun main() { val student = Student("Riya", 92) student.display() } 18. Important OOP Concepts • Class and object: a class defines a type; an object is an instance of that class. • Constructor: initializes an object when it is created. • Inheritance: allows a class to derive behaviour from an open or abstract parent class. • Interface: defines behaviour that implementing classes provide. • Data class: is designed to hold data and provides generated utility functions. • Sealed class: restricts permitted subclasses and represents a limited set of alternatives. • Visibility modifiers: public, private, protected, and internal control access. 19. Exception Handling Exceptions represent unexpected conditions during program execution. Kotlin uses try, catch, finally, and throw to handle them. try { val result = 10 / 0 println(result) } catch (e: ArithmeticException) { println("A calculation error occurred") } finally { println("Finished") } 20. Lambdas and Higher - Order Functions A lambda is an anonymous function that can be stored or passed to another function. A higher - order function takes a function as a parameter or returns one. val values = listOf(1, 2, 3, 4) val doubled = values.map { value - > value * 2 } println(doubled) 21. Coroutines Coroutines support asynchronous programming and help write non - blocking code for tasks such as network requests and database operations. Coroutine functionality is commonly provided by the kotlinx.coroutines library. Important concepts include suspend functions, coroutine scopes, and dispatchers. 22. Java Interoperability Kotlin is designed to work with Java libraries and code. Kotlin can call Java classes and methods, and Java can use Kotlin declarations. When mixing languages, consider nullability, platform types, naming conventions, and generated JVM signatures. 23. Kotlin for Android Kotlin is a major language for Android application development. Android Studio provides project templates, editing, debugging, and build tools. Developers use Kotlin with Android Jetpack libraries and modern UI approaches such as Jetpack Compose. A typical Android project includes application code, resources, a manifest, and Gradle build configuration. 24. Kotlin vs. Java Aspect Kotlin Java Syntax Often more concise Can require more boilerplate Null handling Nullable and non - nullable types Null references need careful handling Interoperability Works with Java APIs Can use Kotlin - generated JVM APIs Data modelling Built - in data classes Regular classes or records, depending on version Common uses Android, backend, multiplatform Backend, enterprise, Android and more 25. Common Uses • Android mobile applications • Backend and server - side services • Desktop applications • Multiplatform projects sharing code across supported targets • Developer tools, scripting, and automation 26. Advantages of Kotlin • Concise syntax can improve readability and reduce repetitive code. • Null - safety features help prevent common programming errors. • Java interoperability supports gradual adoption in existing projects. • Modern language features support expressive application design. • IDE and build - tool support helps with development and debugging. 27. Learning Path for Beginners 1. Learn variables, data types, operators, and input/output. 2. Practise conditions, loops, and functions. 3. Study arrays, strings, collections, and null safety. 4. Learn classes, objects, interfaces, and inheritance. 5. Build small console projects and practise debugging. 6. Choose a direction such as Android or backend development. 7. Explore libraries, testing, version control, and project structure. 2 8 . Conclusion Kotlin combines concise syntax with modern features such as null safety, extension functions, data classes, and coroutines. Its interoperability with Java and strong Android ecosystem make it a practical choice for beginners and experienced developers. Learning the fundamentals and building small projects creates a solid foundation for Kotlin application development.