Skip to main content
Android Development with Kotlin
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
12345
// The entry point of every Kotlin program!
fun main() {
    // This command prints text to the developer console
    println("Hello, Android Developer!") 
}

*(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
12345678910
fun main() {
    // We do NOT need to say this is a String. Kotlin figures it out!
    var playerName = "John Wick" 
    
    // Kotlin automatically knows this is an Integer (whole number)
    var score = 1500 
    
    println(playerName)
    println(score)
}

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
12345678
fun main() {
    var hero = "Batman"
    var city = "Gotham"
    
    // The $ injects the variable perfectly into the string!
    println("The hero $hero protects the city of $city.")
    // Output: The hero Batman protects the city of Gotham.
}

If you need to perform math or logic inside the string, wrap it in curly braces ${}:

kotlin
123
var apples = 5
println("I have ${apples * 2} pieces of fruit.") 
// Output: I have 10 pieces of fruit.

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
1234567
// This is a single-line comment. The computer ignores it.

/*
   This is a multi-line comment.
   You can write paragraphs of explanation here.
   The compiler will ignore all of it!
*/

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 the fun keyword.
kotlin
1234567891011121314
// 1. We define the function
fun greetUser() {
    println("Welcome to the application!")
    println("Loading your profile...")
}

fun main() {
    println("App Started")
    
    // 2. We 'call' or trigger the function
    greetUser() 
    
    println("App Ended")
}

8. Mini Project: The Coffee Shop Script

Let's combine variables, string interpolation, and standard output to build a simple console script.
kotlin
12345678910111213
fun main() {
    // Define the variables
    var customerName = "Alice"
    var coffeeType = "Mocha"
    var price = 4
    
    // Execute the logic using String Interpolation
    println("--- NEW ORDER ---")
    println("Customer: $customerName")
    println("Order: One large $coffeeType.")
    println("Total Due: $$price.00")
    println("Thank you, $customerName, your $coffeeType is being prepared!")
}

9. Common Mistakes

  • Forgetting the main Function: If you write a bunch of variables and println commands at the top level of a file, outside of a fun 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. 1. Write a main function. Inside it, declare a variable containing your favorite color.
  1. 2. Use println and 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 the fun 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.

17. Next Chapter Recommendation

We know how to create variables, but we need to understand exactly how the computer categorizes data. What happens if a variable's data should never be allowed to change? Proceed to Chapter 4: Variables, Data Types, and Operators in Kotlin.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·