Android: Kotlin Talk #002 - ABC Kotlin Basics

Jayaprakash RJayaprakash R
2 min read

Kotlin's core syntax is the foundation of the language.

Kotlin’s core syntax lays the foundation for clean, maintainable, and expressive code.
Here are some quick essentials ⬇️

🔹 Core Concepts

  • Variables: val (immutable) & var (mutable)

  • Data types: Int, String, Boolean, Double

  • Control flow: if/else, powerful when expression

  • Loops: for, while (over ranges, collections, arrays)

🔹Example

Kotlin snippet is a neat little showcase of basic language features:

  1. Variable declaration

     val name: String = "Kotlin" // Immutable (read-only)
     var version: Double = 1.9   // Mutable
    
    • val → read-only, can’t be reassigned.

    • var → mutable, can be updated.

  2. Control flow with if

     if (version > 1.8) {
         println("$name is up to date.")
     } else {
         println("$name needs an update.")
     }
    
    • Uses string templates ($name) for cleaner output.

    • Works just like in Java, but is also an expression in Kotlin (could return a value).

  3. Control flow with when

     when (name) {
         "Kotlin" -> println("Current language is Kotlin.")
         "Java" -> println("Legacy language detected.")
         else -> println("Unknown language.")
     }
    
    • More readable than multiple if-else statements.

    • No need for break like in Java’s switch.

  4. for loop

     for (i in 1..5) {
         print("$i ")
     }
    
    • 1..5 creates a closed range (includes both 1 and 5).

    • Prints numbers in sequence with spaces.

✅ Best Practices

  • Prefer val over var → promotes immutability & safer code

  • Use type inference for concise code, but declare types when it improves readability

  • Use when for complex conditions instead of multiple if–else if blocks

  • Leverage string templates for cleaner formatting

⚠️ Common Pitfalls

  • Using var unnecessarily → can cause unexpected side effects

  • Overly complex nested if–else instead of when

  • Forgetting 1..5 in for loops is inclusive of 5

💡 Mastering these small but impactful details can help you write Kotlin code that’s cleaner, safer, and more maintainable.

#Kotlin #AndroidDevelopment #MobileApps #CleanCode #ProgrammingTips

Happy Coding !!!

#Kotlin #AndroidDevelopment #KotlinForAndroid #JetpackCompose #AndroidDev #MobileDevelopment #KotlinProgramming #AndroidStudio #Coroutines #KotlinDeveloper #NullSafety #JetpackLibraries #ModernAndroidDevelopment #AndroidJetpack #MobileAppDevelopment #KotlinTips

0
Subscribe to my newsletter

Read articles from Jayaprakash R directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jayaprakash R
Jayaprakash R

🚀 Android Lead | Mobile Tech Strategist | Scalable Architecture Advocate 👋 Hi, I’m Jayaprakash — an aspiring Android Tech Lead with over a decade of experience in crafting high-performance, user-centric mobile applications that drive measurable impact and scale seamlessly across customer bases. I specialize in transforming manual workflows into efficient, automated CI/CD pipelines using tools like GitLab, Jenkins, JUnit, and JFrog Artifactory, while ensuring reliable integration with Oracle databases. My approach accelerates software delivery cycles through agile methodologies, boosting productivity and reducing time-to-market.