Getting Started with Kotlin Programming(Part-1)

Richa Sharma
5 min readJun 5, 2022
Image source : Google

In this article we’re going to start understanding with some basic concepts from Kotlin like:

  1. Basic Syntax
  2. Functions
  3. Null Safety
  4. val, var, lateinit, const

Kotlin Programming Overview

  • Kotlin is a modern statically typed programming language used by over 60% of professional Android developers that helps boost productivity, developer satisfaction, and code safety.
  • It is focused on interoperability, safety, clarity, and tooling support.

Why Kotlin?

Image source : Google
  1. Modern, concise and safe programming language — Easy to pick up, so you can create powerful applications immediately.

2. A productive way to write server‑side applications — Compatible with the Java ecosystem. Use your favorite JVM frameworks and libraries.

3. Cross-platform layer for native applications — Share application logic between web, mobile, and desktop platforms while keeping an experience native to users.

4. Big, friendly and helpful community — Kotlin has great support and many contributors in its fast-growing global community. Enjoy the benefits of a rich ecosystem with a wide range of community libraries.

So, let’s begin

Basic Syntax

  • Imports in Kotlin — Package specification should be at the top of the source file.
Example
  • Print Output — Print (“ ”) for printing standard output and println(“”) prints its arguments and adds a line break, so that the next thing you print appears on the next line.
Example

Introduction to Variables :

  • val and var both are used to declare a variable.
  • Read-only local variables are defined using the keyword val. They can be assigned a value only once.
  • The object stored using val cannot be changed, val is immutable
  • Syntax :
  • here if you write like this it will compiler will say val cannot be reassigned.
  • So if we want to reassign something to variable we should use var.
  • The value of a variable that is declared using var can be changed anytime throughout the program. var is also called mutable a, as there value can be changed anytime.
  • Const — The const keyword is used to declare those properties which are immutable in nature i.e. these properties are read-only properties.

What is difference between const and val ?

  • the value of the val variable is initialized at runtime and const at compile time.
  • That means that consts can never be assigned to a function or any class constructor, but only to a String or primitive.
  • lateinit — The “lateinit” keyword in Kotlin as the name suggests is used to declare those variables that are guaranteed to be initialized in the future.
  • The lateinit keyword is used for late initialization of variables.
private lateinit var name: String
fun initialzeName(){
name = "Alan"
// initializing name
}
  • If we don't initialize lateinit it will say UninitializedPropertyAccessException: lateinit property name has not been initialized

Functions in Kotlin

  • Kotlin functions are declared using the fun keyword
  • Calling member functions uses dot notation:
Sample().add()
// create instance of class Sample and call add()

Null Safety in Kotlin

  • In Kotlin by default, every type is not Nullable.
  • Suppose in the variable name which is of type String we try to assign null it will give an error as “Null cannot be the value of non- null type String”.
var name: String = null
  • But what can we do if we want to use null in our code?
  • So what we can do is we can use “?” behind the datatype String and then we can assign null to the variable name.
var name: String? = null
  • so String? is Nullable String datatype which can be a string or it can be null.
  • Now if we have to assign null in our code may be at some point in our code we are not sure anymore that object is null.If now we try to perform length of var name this will give an error.
name.length
  • This is because Only safe(?.) or non-nullable asserted (!!.) calls are allowed on the nullable receiver of type String?. In this case, Kotlin will not give a null pointer exception but the compiler will see that this is as invalid.
  • For this we can follow 2 way :
name!!.length
  1. using “!!” operator — now what this will do is that it will tell the compiler you are sure that the String object is not null.So we should not use this unless you are sure that the object cannot be null. This will give Null Pointer Exception.
  2. using “?” operator — if var is assigned to null it will give length as null but if var is assigned with some value of String it will give its length.
var name : String? = null
name?.length
//this will give output as null
  • but if in this case, var assign with String value than :
var name : String? = "Adam"
name?.length
//this will give output as 4

We will go through some advance topic in next blog.

Source : Google

Happy Reading :)

--

--