codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🎯

Kotlin

45 / 68 topics
44Kotlin for Android Development45Android Kotlin Basics46Kotlin Android Extensions47ViewModel and LiveData48Room Persistence Library
Tutorials/Kotlin/Android Kotlin Basics
🎯Kotlin

Android Kotlin Basics

Updated 2026-04-20
3 min read

Android Kotlin Basics

Introduction

Kotlin is a modern, statically typed programming language that has become increasingly popular for Android development. It offers concise syntax, powerful features, and seamless interoperability with Java. This tutorial will cover the basics of using Kotlin in Android development, including setup, basic syntax, data types, control structures, functions, and object-oriented programming.

Setting Up Your Development Environment

Before you start writing Kotlin code for Android, ensure your development environment is set up correctly:

  1. Install Android Studio: Download and install the latest version of Android Studio.
  2. Create a New Project:
    • Open Android Studio.
    • Select "Start a new Android Studio project."
    • Choose "Empty Activity" and click "Next."
    • Configure your project settings (name, package name, save location) and select Kotlin as the language.
    • Click "Finish."

Basic Syntax

Variables and Data Types

Kotlin supports various data types, including:

  • Primitive Types: Int, Double, Boolean, etc.
  • Reference Types: String, List, Map, etc.

Variable Declaration

// Immutable variable (val)
val immutableVar: Int = 10

// Mutable variable (var)
var mutableVar: String = "Hello"
mutableVar = "World"

Strings

Kotlin provides several ways to work with strings:

  • String Templates:

    val name = "Alice"
    println("Hello, $name!") // Output: Hello, Alice!
    
  • Raw Strings:

    val rawString = """
        |This is a raw string.
        |It can span multiple lines.
        """.trimMargin()
    

Control Structures

Kotlin supports standard control structures like if, when, for, and while.

If-Else Statements

val number = 10
if (number > 5) {
    println("Number is greater than 5")
} else {
    println("Number is less than or equal to 5")
}

When Expressions

when is a powerful alternative to switch in Kotlin:

val number = 2
when (number) {
    1 -> println("One")
    2 -> println("Two")
    else -> println("Other")
}

Functions

Functions in Kotlin are defined using the fun keyword. They can have parameters and return types.

Basic Function

fun greet(name: String): String {
    return "Hello, $name!"
}

// Calling the function
println(greet("Alice")) // Output: Hello, Alice!

Single-Expression Functions

For functions with a single expression, you can omit the curly braces and return keyword:

fun greet(name: String) = "Hello, $name!"

Object-Oriented Programming

Kotlin supports object-oriented programming principles such as classes, inheritance, interfaces, and polymorphism.

Classes

Classes in Kotlin are declared using the class keyword. By default, they are final unless marked with open.

open class Animal(val name: String) {
    open fun makeSound() {
        println("Some generic sound")
    }
}

Inheritance

Subclasses inherit from a base class using the : symbol and can override methods.

class Dog(name: String) : Animal(name) {
    override fun makeSound() {
        println("Woof!")
    }
}

val myDog = Dog("Buddy")
myDog.makeSound() // Output: Woof!

Interfaces

Interfaces in Kotlin are similar to Java interfaces but can also include default implementations.

interface Movable {
    fun move()
    fun stop() {
        println("Stopped moving.")
    }
}

class Car : Movable {
    override fun move() {
        println("Car is moving.")
    }
}

Best Practices

  1. Use val Instead of var When Possible: Immutable variables are safer and easier to reason about.
  2. Avoid Nullability Issues: Use Kotlin's null safety features, such as nullable types (String?) and the safe call operator (?.).
  3. Leverage Extensions: Extend existing classes with new functionality without modifying their source code.
  4. Use when Instead of Multiple if-else Statements: It makes your code more readable and concise.

Conclusion

Kotlin provides a robust set of features that make Android development more efficient and enjoyable. By understanding the basics covered in this tutorial, you'll be well on your way to writing clean, maintainable Kotlin code for Android applications. As you progress, continue exploring advanced topics like coroutines, Jetpack Compose, and more to enhance your skills further.


PreviousKotlin for Android DevelopmentNext Kotlin Android Extensions

Recommended Gear

Kotlin for Android DevelopmentKotlin Android Extensions