Kotlin Tutorial
Estimated reading: 4 minutes 28 views

🧱 Kotlin Syntax & Language Basics – Variables, Data Types, Operators & More

🧲 Introduction – Learn the Building Blocks of Kotlin Programming

Before diving into classes, functions, and control structures, you must master the core Kotlin syntax and language fundamentals. This foundation ensures you can write clear, concise, and idiomatic Kotlin code from the start.

From declaring variables with val and var to using basic data types, operators, and inline comments, Kotlin’s clean syntax is designed to reduce boilerplate and increase code safety.

🎯 In this guide, you’ll learn:

  • How to write syntactically correct Kotlin code
  • How to use variables, constants, and type inference effectively
  • Which data types and literals are available in Kotlin
  • How to apply arithmetic, logical, and relational operators
  • How to use comments and reserved keywords

📘 Topics Covered

🧩 Topic📖 Description
📝 Kotlin – SyntaxGeneral structure of Kotlin programs, formatting, indentation, and semicolons.
📤 Kotlin – OutputDisplaying data using println() and print() functions.
💬 Kotlin – CommentsSingle-line (//) and multi-line (/* */) comment syntax.
🗝️ Kotlin – KeywordsReserved words that cannot be used as variable or function names.
📦 Kotlin – Variables (val vs var)Immutable vs mutable variables and declaration rules.
🔢 Kotlin – Data TypesBasic types like Int, Double, Char, String, Boolean.
🧠 Kotlin – Type InferenceAutomatic data type detection during variable initialization.
Kotlin – OperatorsArithmetic, comparison, logical, assignment, and bitwise operators.
🎯 Kotlin – Constants & LiteralsDeclaring read-only values and understanding literal types in code.

🔍 Detailed Sections & Examples

📝 Kotlin – Syntax

fun main() {
    println("Hello, Kotlin Syntax!")
}

💡 Explanation:

  • fun defines a function.
  • main() is the program entry point.
  • println() prints text to console.
  • No semicolons needed—Kotlin handles it automatically.

📤 Kotlin – Output

print("Hello")
println(" World")

📌 Output: Hello World

  • print() does not add a newline.
  • println() prints and adds a newline.

💬 Kotlin – Comments

// This is a single-line comment

/*
This is a 
multi-line comment
*/

🧠 Use comments for clarification, documentation, or debugging help.


🗝️ Kotlin – Keywords

Examples of reserved keywords in Kotlin:

val, var, fun, class, if, else, for, while, return, object, try

⚠️ These cannot be used as variable names unless enclosed in backticks:

val `class` = "Reserved but escaped"

📦 Kotlin – Variables (val vs var)

val name = "Alice"  // Immutable
var age = 30        // Mutable

📌 val ➝ read-only
📌 var ➝ reassignable


🔢 Kotlin – Data Types

TypeExample
Intval x = 10
Doubleval pi = 3.14
Charval ch = 'A'
Stringval name = "Joe"
Booleanval isOn = true

🧠 Kotlin – Type Inference

val city = "London"  // Inferred as String
val year = 2025      // Inferred as Int

Kotlin automatically determines the type based on the value.


➗ Kotlin – Operators

Arithmetic Operators:

val sum = 10 + 5
val mod = 10 % 3

Relational:

val isEqual = (5 == 5)

Logical:

val result = (5 > 3) && (2 < 4)

Assignment:

var a = 10
a += 5  // a becomes 15

🎯 Kotlin – Constants & Literals

const val PI = 3.14159
  • const val is used for compile-time constants
  • Supports Int, String, Boolean, and basic literals only

📌 Summary – Recap & Next Steps

Understanding Kotlin’s syntax and basic language elements helps you write efficient, readable, and safe code. With type inference, expressive operators, and streamlined output, Kotlin offers a productive coding experience right from the start.

🔍 Key Takeaways:

  • Kotlin code is clean—no need for semicolons or verbose syntax.
  • Use val for immutability and var for reassignable variables.
  • Kotlin supports all major data types with smart type inference.
  • Comments, keywords, and constants follow standard conventions.

⚙️ Practical Use Cases:

  • Define and manipulate variables in business logic
  • Write conditions, calculations, and string manipulations
  • Prepare for functions, classes, and OOP constructs

❓ Frequently Asked Questions

What’s the difference between val and const val?
val is runtime-assigned, const val is compile-time constant used at the top level or inside objects.


Is Kotlin statically typed?
✅ Yes. Despite type inference, Kotlin is statically typed—types are checked at compile time.


Can I reassign a val variable?
✅ No. val is immutable. Use var if reassignment is needed.


Do I have to declare variable types explicitly?
✅ No, thanks to type inference, but you can for clarity or readability.

val age: Int = 30

What happens if I try to use a keyword as a variable?
✅ It will cause a compile error unless wrapped in backticks:

val `return` = "keyword as variable"

Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Kotlin Syntax & Language Basics

Or Copy Link

CONTENTS
Scroll to Top