Kotlin Basics Made Easy
This document provides a list of basic Kotlin programming commands and concepts to help you get started with writing and executing Kotlin code.
Program Structure
fun main() { ... }: Main function where the program starts execution.//: Comment line.
Variables
val variableName = value: Define a read-only variable.var variableName = value: Define a mutable variable.variableName: Access the value of a variable.
Data Types
Int: Integer type.Double: Floating-point type.String: String type.Boolean: Boolean type.
Input and Output
println(text): Print text to the console.readLine(): Read input from the user.
Conditional Statements
if (condition) { ... }: Basic if statement.if (condition) { ... } else { ... }: If-else statement.when (variable) { ... }: Switch statement equivalent.
Loops
for (item in collection) { ... }: For loop.while (condition) { ... }: While loop.do { ... } while (condition): Do-while loop.
Functions
fun functionName(parameters): ReturnType { ... }: Define a function.functionName(arguments): Call a function.
Collections
val listName = listOf(value1, value2, value3): Define a read-only list.val mutableListName = mutableListOf(value1, value2, value3): Define a mutable list.val mapName = mapOf(key1 to value1, key2 to value2): Define a read-only map.
String Operations
string.length: Get the length of a string.string.substring(startIndex, endIndex): Extract a substring.
Null Safety
var nullableVar: String? = null: Declare a nullable variable.nullableVar?.length: Safe call operator to avoid NullPointerException.
Exception Handling
try { ... } catch (e: Exception) { ... }: Handle exceptions.
Lambda Functions
val lambdaName: (Type) -> ReturnType = { parameters -> body }: Define a lambda function.lambdaName(arguments): Call a lambda function.
These commands and concepts should help you get started with basic Kotlin programming.
Let me know if you need any further adjustments!