In 2019 Google announced that 'Android Development will be increasingly Kotlin first'
Which begs the question to every new Android developer, what is Kotlin? In this article I will cover an introduction to it and cover a few basics. This will be curated to be as beginner friendly as possible.
What is Kotlin?
Kotlin is a JVM language(languages that run on the Java Virtual Machine). It's Open Source and mainly targets Android even though it does the same for JavaScript and Native.
Key features of Kotlin
- Supports Immutability
-An mutable object /variable can be modified after its created while an immutable object is one that once its created it can't be changed. Unlike other Functional Programming languages, Kotlin does not force but instead encourages immutability. This is done through val which is read only and var which is mutable.
data class Car(val name: String, val id: Int)
val car =Brand("Merc", 1)
car.brand = "Subaru" // won't compile as `val` cannot be reassigned
car = Brand("Subaru", 2) // won't compile as `val` cannot be reassigned
- Nullability in Kotlin
A Null reference is a reference to an empty area in memory even though the value isn't zero. Kotlin's type system is built to drastically reduce or eliminate null references in code as there's a huge possibility that the app will crush during run time. This is because Kotlin does not allow property access and method calls on nullable variables.
Kotlin prevents NullPointerExceptions(they occur when we try calling an object or accessing a property that has a variable which is null). When declaring a reference, you have to initially say whether it can hold null values or not.
All variables in Kotlin are non-nullable by default. This means that if you try assigning a null value to a variable without allowing it to hold a null value initially then there will be a compile time error.
val food: String= "Traditional and Fast"
food=null //compile time error
To allow it to carry a null value , you put a question mark after the type declaration
val nullableFood: String?="Traditional and Fast"
nullableFood = null // compiles succesfully
- Object Oriented
It provides support for Object Oriented Programming. We are able to use classes and objects which are the building blocks of OOP languages.
Class Car{
var brand= "Merc"
var model="S Class"
var year=2020
}
- It's fully interoperable with Java
Because Kotlin runs on JVM and supports Java libraries, this allows developers to integrate the two languages. Existing Java code can be called from Kotlin and vice versa.
Basics of programming in Kotlin
- Declaring Variables
val=immutable value
var=mutable value
var veggie: String ="Kale"
veggie="tomato"
By using var it means that you can reassign values to veggie.
val lackOfIron: String = "Anaemia"
By using val it means that we cannot modify the value assigned initially
- Functions
fun main() {
println( "Hello World! " )
}
Calling a function
fun main() {
myFunction()
}
Printing many functions:
fun main() {
println( "Happy Birthday " )
println( "Be happy " )
println( "Strawberry Shortcake" )
}
Mathematical functions
fun main() {
println( 1+1 ) //function body
}
fun sum(x: Int, y: Int) : Int{
return a+b
}
fun sum(x: Int, y: Int)= x + y //shorter syntax for return values
- Classes and Objects
class Car{
var brand="Mercedes "
var model=" S class"
var year="2020"
}
Using constructors and Class Functions
A constructor is a special type of member function of a class which initializes objects of a class.
class Car( var brand: String, var model: String, var year: Int) {
//class function
fun drive() {
println("Drive safely")
}
}
fun main() {
val firstCar=Car( "Mercedes" ,"S Class" ," 2021" )
Arrays
We use the arrayOf() function to create arrays in Kotlin.
var citrusFruits=arrayOf("Oranges" , "Tangerine" , "Lime" , "Lemon")
- Conditionals
if(condition) {
// block of code to be executed if condition is true
}else{
//block of code to be executed if condition isn't true
}
//example
fun main(pass > 40) {
if (marks<40) {
println("Fail")
}else{
println("Pass")
}
- Operators
Arithmetic Operators
Addition x+y
Subtraction x-y
- Multiplication (x*y)
Division x / y
Modulus x%y
Assignment Operators
These are used to assign values to variables
(-=) Reduce the value of a variable
(*=) Multiplies value of a variable
(/=) Divides the value of a variable
(%) Returns the remainder
Comparison Operators
These are used to compare two values and return a boolean(True or False)
(==) Equal to
(!=) Not Equal to
(>) Greater than
(<) Less than
(>=) Greater than or equal to
(<=) Less than or Equal to
Logical Operators
These are used to determine logic between variables and values
&& Logical and
Returns True only if both operands evaluate to be true
if ((5 < 10) && (10 < 5)) {
println("True")
}
||Logical or
Returns true if either of the Boolean expression is true
if ((5 < 10) || (10 < 5)) {
println("True")
}
- Data Types
val myNum=10 //Int -whole numbers
val myDoubleNum=8.76 //Double-floating point numbers
val myLetter='A' //char - single characters
val myBoolean=true //Boolean -true or false
val myText= "Hello World" //String -sequence of characters.
In later articles we will cover each of this aspects deeply .Feel free to comment ,add on or correct any mistake. Happy reading.