Android Development : 02 :Kotlin , The basics


Why use Kotlin:
1. Complete null safety :
Kotlin has built-in null safety (?
operator), which helps avoid NullPointerException
(the "billion-dollar mistake").
var name: String? = null // Safe in Kotlin
In Java, you'd need to add many null checks manually.
NullPointerException
Null Safety
NullPointerException
at compile time by making it clear which variables can be null and which cannot. In Java, any object reference can be null
, and using it without checking causes a crash (NullPointerException
). Kotlin eliminates this issue using nullable and non-nullable types.In Kotlin:
Non-nullable type:
it cannot hold a
null
valueDeclared without a
?
(built in null safety operator)Compiler won’t allow null assignment
var name: String = "John"
// name = null ❌ Compile-time error
Nullable type:
it can hold a
null
valueDeclared with a
?
Can store either a value or
null
var name: String? = "John"
name = null ✅ // allowed
There are 3 type of operators:
a. Safe Call (?.
)
println(name?.length) // Returns null if name is null
b. Elvis Operator (?:
)
val length = name?.length ?: 0 // If name is null, use 0
c. Not-null Assertion (!!
)
println(name!!.length) // Throws NPE if name is null (use with caution)
d. Safe Cast (as?
)
val obj: Any? = "Hello"
val str: String? = obj as? String
Null safety means Kotlin code won’t be even compile if we try to acess an element on null object.
Nullable objects are created explicitly in Kotlin
2.Use All Java Libraries in Kotlin too:
- Kotlin is 100% interoperable with Java. You can call Java code from Kotlin and vice versa.
3.Kotlin has coroutine:
- Kotlin provides coroutines to write async code easily and more clearly than Java's
AsyncTask
or callbacks.
GlobalScope.launch {
val data = fetchData()
}
4.Google support:
Has official support of Google. Since 2017, Google has made Kotlin the preferred language for Android development.
Jetpack Compose (modern UI toolkit) is built with Kotlin in mind.
5. Less code in Kotlin:
Kotlin is more compact .
Kotlin reduces boilerplate code significantly.
Less code = fewer bugs + better readability.
Programming with Kotlin:
Function:
A function is a segment of a program that performs a specific task. Your program may have one or more functions.
Define a function vs Calling a function :In your code, you define a function first. That means you specify all the instructions needed to perform that task.
Once the function is defined, then you can call that function, so the instructions within that function can be performed or executed.
Function names should follow the camel case convention, where the first word of the function name is all lower case. If there are multiple words in the name, there are no spaces between words, and all other words should begin with a capital letter.
There should be a space before the opening curly brace.
Variable:
In the apps that you use on your phone, notice that some parts of the app stay the same, while other parts change. For example, the names of the categories within the Settings app stay the same – Network & internet, Connected devices, Apps, and more. On the other hand, if you look at a news app, the articles will change often. The article name, source, time posted, and images change.
How do you write your code so that content changes over time? You can't rewrite the code in your app every time there are new articles to post, which happens every day, every hour, and every minute!
In this codelab, you learn how to write code that uses variables so that certain parts of your program can change without having to write a whole new set of instructions.
fun main() { val count: Int = 2 println(count) }
The Kotlin compiler knows that you want to store
2
(a whole number integer) into the variablecount
, so it can infer that thecount
variable is of typeInt
. Convenient, right? This is one example of how writing Kotlin code is more concise!fun main() { val count= 2 println(count) }
Note: If you don't provide an initial value when you declare a variable, you must specify the type.
In this line of code, no initial value is provided, so you must specify the data type:
val count: Int
In this line of code, an assigned value is provided, so you can omit the data type:
val count = 2
fun main() {
val numberOfPhotos = 100
val photosDeleted = 10
println("$numberOfPhotos photos")
println("$photosDeleted photos deleted")
println("${numberOfPhotos - photosDeleted} photos left")
}
Var vs Val:
Val:
Immutable Reference
Stands for "value"
Read-only variable (like
final
in Java) {both prevents reassignment, which is the same idea.}You cannot reassign a new value once it's set
final int x = 10;
x = 20; // ❌ Error
val x = 10
x = 20 // ❌ Error
However, just like in Java, if val
refers to a mutable object, the contents can still change:
val list = mutableListOf(1, 2)
list.add(3) // ✅ Allowed
list = listOf(4) // ❌ Error: Val cannot be reassigned
Var:
Mutable Reference
Stands for "variable"
Mutable variable — you can reassign it
var age = 25
age = 26 // ✅ Allowed
Feature | val | var |
Mutability | Immutable (read-only) | Mutable (can be updated) |
Reassignment | Not allowed | Allowed |
Use Case | Constants, fixed values | Changing values |
Subscribe to my newsletter
Read articles from Dibyashree Chakravarty directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
