Kotlin Basic Syntax Tutorial: main Function, Variables, Data Types, and Null Safety


This Kotlin Basic Syntax tutorial explains the core building blocks of Kotlin programming, including the main function, variables using val and var, data types, type inference, comments, string templates, and nullable versus non-nullable types. The chapter provides clear explanations, coding best practices, and practical examples to help beginners write clean, safe, and readable Kotlin programs.

Kotlin Basic Syntax – Complete Tutorial

main Function

The main function is the entry point of a Kotlin program. Execution always starts from this function.

Example


fun main() {
println("Welcome to Kotlin")
}

Best Practices

  1. Keep the main function minimal.
  2. Move business logic into separate functions.

Variables: val and var

Kotlin provides two ways to declare variables.

val (Immutable Variable)

  1. Value cannot be changed after assignment.
  2. Similar to final in Java.

val language = "Kotlin"

var (Mutable Variable)

  1. Value can be changed.

var version = 1
version = 2

Best Practices

  1. Prefer val by default.
  2. Use var only when mutation is required.

Data Types

Kotlin is a statically typed language, but it handles types intelligently.

Common Data Types

  1. Int
  2. Long
  3. Double
  4. Float
  5. Boolean
  6. Char
  7. String

Example


val age: Int = 30
val salary: Double = 45000.50
val isActive: Boolean = true

Best Practices

  1. Use explicit types for clarity in complex logic.
  2. Avoid unnecessary type casting.

Type Inference

Kotlin automatically infers the data type based on the assigned value.

Example


val count = 10
val name = "Kotlin"
val price = 99.99

Best Practices

  1. Rely on type inference for simple declarations.
  2. Declare types explicitly in public APIs.

Comments

Comments are used to explain code and improve readability.

Single-Line Comment


// This is a single-line comment

Multi-Line Comment


/*
This is a
multi-line comment
*/

Best Practices

  1. Write comments to explain why, not what.
  2. Keep comments updated with code changes.

String Templates

String templates allow embedding variables and expressions directly inside strings.

Example


val user = "Muni"
val age = 35
println("User name is $user and age is $age")

Expression Example


println("Next year age will be ${age + 1}")

Best Practices

  1. Use string templates instead of string concatenation.
  2. Keep expressions simple inside templates.

Nullable and Non-Nullable Types

Kotlin distinguishes between nullable and non-nullable types to avoid NullPointerException.

Non-Nullable Type


var city: String = "Mumbai"

Nullable Type


var address: String? = null

Safe Call Operator


println(address?.length)

Elvis Operator


val length = address?.length ?: 0

Non-Null Assertion (Use with caution)


println(address!!.length)

Best Practices

  1. Avoid !! whenever possible.
  2. Use safe calls and Elvis operator for safer code.

Summary

In this chapter, you learned Kotlin’s basic syntax, including the main function, variable declarations, data types, type inference, comments, string templates, and null safety. Mastering these concepts is essential for writing clean, safe, and maintainable Kotlin code.