Kotlin Simplified (Part -1 Basic syntax)

Kotlin is a modern but already mature programming language aimed to make developers happier. It’s concise, safe, interoperable with Java and other languages, and provides many ways to reuse code between multiple platforms for productive programming.

Package definition and imports

package my.demo

import kotlin.text.*

// Same as other programing language

Program entry point

fun main() { 
println("Hello world!")
// main function can be run without arguments.
fun main(args: Array<String>) {
// Arguments are same as java Main Method.

Print to the standard output

print("Hello ")
println("world !")
Output :- Hello world!


fun sum(a: Int, b: Int): Int {
return a + b
//A function body can be an expression. Its return type is inferred.fun sum(a: Int, b: Int) = a + b
fun printSum(a: Int, b: Int): Unit {
println("sum of $a and $b is ${a + b}")
//Unit return type can be omitted.fun printSum(a: Int, b: Int) {
println("sum of $a and $b is ${a + b}")


val a: Int = 1 //  immediate assignment
val b = 2 // `Int` type is inferred
val c: Int // Type required when no initializer is provided
c = 3 // deferred assignment
Output :- a = 1, b = 2, c = 3
var x = 5 // `Int` type is inferred
x += 1
Output :- x = 6


// This is an end-of-line comment  ( single-line comments )/* This is a block comment         ( multi-line (block) comments )
on multiple lines. */
/* The comment starts here ( nested block comments )
/* contains a nested comment */
and ends here. */

String templates

var a = 1     // simple name in template:
val s1 = "a is $a"
a = 2 // arbitrary expression in template:
val s2 = "${s1.replace("is", "was")}, but now is $a"
Output :- a was 1, but now is 2

Conditional expressions

// Just like any programming language If Else are same. if (a > b) {
return a
}else {
return b
(In Kotlin, if can also be used as an expression.)
if (a > b) a else b

for loop

val items = listOf("apple", "banana", "kiwifruit")for (item in items) {                 for (index in items.indices) {                   
println(item) ( OR ) println("item at $index is
} }
Output:- Output:-
apple item at 0 is apple
banana item at 1 is banana
kiwifruit item at 2 is kiwifruit

while loop

while (index < items.size) {
println("item at $index is ${items[index]}")
Output :- item at 0 is apple
item at 1 is banana
item at 2 is kiwifruit

when expression

fun describe(obj: Any): String =when (obj) {
1 -> "One"
-> "Greeting"
Long -> "Long"
!is String -> "Not a string"
-> "Unknown"
fun main() {
Output :-
One Greeting Long Not a string Unknown


val x = 10
if (x in 1..100) {
println("fits in range")
for (x in 1..5) {
for (x in 1..10 step 2) {
for (x in 9 downTo 0 step 3) {

Type checks and automatic casts

fun getStringLength(obj: Any): Int? {
if (obj is String) {
return obj.length
// `obj` is automatically cast to `String` in this branch

return null
// `obj` is still of type `Any` outside of the type-checked branch
fun getStringLength(obj: Any): Int? {if (obj !is String) return nullreturn obj.length}// `obj` is automatically cast to `String` in this branch

I am a programmer and a constant learner. So I post things I'm learning and a lot I already know. Programming-based content continues as a priority.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store