CHAPTER 03
Beginner
Kotlin Programming Basics
Updated: May 16, 2026
20 min read
# CHAPTER 3
Kotlin Programming Basics
1. Introduction
Before you can build complex Android applications with buttons, databases, and network calls, you must learn how to speak to the machine. The native language of modern Android development is Kotlin. Kotlin is an elegant, concise, and incredibly powerful programming language designed by JetBrains. In this chapter, we will master Kotlin Programming Basics. We will step away from the Android UI for a moment and focus purely on the logic: declaring variables, understanding data types, writing functions, and utilizing Kotlin's brilliant Type Inference engine.2. Learning Objectives
By the end of this chapter, you will be able to:- Write and execute basic Kotlin scripts.
- Understand the concept of Type Inference.
- Use String interpolation to inject variables into text dynamically.
- Write basic Kotlin functions to encapsulate logic.
-
Use the
println()command for standard input/output.
3. The main Function
Every Kotlin program requires a starting point. When a Kotlin file is executed, the computer aggressively searches for a specific function called main. If it doesn't find it, the program will not run.
kotlin
*(Note: Unlike Java or C++, Kotlin does NOT require semicolons ; at the end of every line!)*
4. Variables and Type Inference
A variable is a named box in the computer's memory where you can store data. In older languages like Java, you had to explicitly tell the computer exactly what type of data the box was going to hold before you put anything in it. Kotlin is smarter. It uses Type Inference. It looks at the data you are trying to store, and automatically figures out the type for you!
kotlin
5. String Interpolation (The $ Symbol)
In many languages, if you want to combine variables with text, you have to use ugly plus signs (e.g., "My name is " + name + " and I am " + age).
Kotlin uses String Interpolation. You simply place a dollar sign $ directly inside the quotes, followed by the variable name!
kotlin
If you need to perform math or logic inside the string, wrap it in curly braces ${}:
kotlin
6. Comments in Kotlin
Comments are notes written in the code that the computer completely ignores. They are used to explain complex logic to other human developers.
kotlin
7. Basic Functions
A function is a reusable block of code that performs a specific task. Instead of writing the same 10 lines of code over and over, you write it once in a function, and "call" the function whenever you need it. In Kotlin, functions are declared using thefun keyword.
kotlin
8. Mini Project: The Coffee Shop Script
Let's combine variables, string interpolation, and standard output to build a simple console script.
kotlin
9. Common Mistakes
-
Forgetting the
mainFunction: If you write a bunch of variables andprintlncommands at the top level of a file, outside of afun main() { ... }block, the Kotlin compiler will throw massive errors. Code that executes must be contained within a function!
- Using Semicolons: While Kotlin *allows* you to put a semicolon at the end of a line, doing so is considered terrible practice and violates Kotlin styling guidelines. Trust the compiler; drop the semicolons.
10. Best Practices
-
Naming Conventions: Variable and function names in Kotlin should always use camelCase. This means the first word is lowercase, and every subsequent word is capitalized with no spaces (e.g.,
playerName,calculateTotalScore()).
11. Exercises
-
1.
Write a
mainfunction. Inside it, declare a variable containing your favorite color.
-
2.
Use
printlnand String Interpolation ($) to print: "My favorite color is [your color here]".
12. Coding Challenges
Challenge: Write a Kotlin script that calculates the area of a rectangle. Create two variables:width (set to 10) and height (set to 5). Create a third variable named area that multiplies the two together (width * height). Finally, print a beautiful sentence using String interpolation: "A rectangle with width 10 and height 5 has an area of 50."
13. MCQ Quiz with Answers
Question 1
In Kotlin, what powerful compiler feature allows you to type var score = 100 without explicitly declaring that score is an Integer?
Question 2
Which symbol is utilized within a Kotlin String literal to seamlessly inject a variable directly into the text (e.g., "Welcome back, ___name" )?
14. Interview Questions
- Q: Explain the concept of "Type Inference" in Kotlin. How does it improve developer productivity compared to strictly typed legacy languages like Java or C++?
-
Q: Contrast traditional string concatenation (using the
+operator) with Kotlin's String Interpolation. What specific readability advantages does interpolation provide?
-
Q: Describe the absolute necessity of the
fun main()block within a standalone Kotlin application.
15. FAQs
Q: How can I practice Kotlin code without opening Android Studio? A: You can practice purely in your web browser! Go to play.kotlinlang.org. This is an official sandbox provided by JetBrains where you can type Kotlin code and hit the "Run" button to see the output instantly in the console. It is perfect for practicing logic without the heavy weight of the Android UI!16. Summary
In Chapter 3, we took our first steps into the native language of Android. We learned the structural necessity of thefun main() entry point. We embraced Kotlin's modern architecture, abandoning tedious semicolons and leveraging the brilliance of Type Inference to cleanly declare memory variables. We engineered highly readable output strings utilizing String Interpolation, and organized our logic into reusable blocks utilizing the fun keyword.