DEV Community

Chetan
Chetan

Posted on • Edited on

Basics of Kotlin - Part 1

In the last article, we try to learn about Gradle and App Folder. If you haven't read the previous article, visit this link and read it
Exploring the Android Studio
Before we start working on Android, let's learn about Kotlin first. In the next few articles, we will try to learn the basics of Kotlin like the loop, conditional statements, function, class.

Kotlin:

Kotlin is an open-source, statically typed and general-purpose language that runs on Java Virtual Machine(JVM). It was developed by JetBrains Team, the official designer of the most intelligent Java IDE, named Intellij IDEA. It was officially released in February 2016 and in 2017 Google announced it as the official language for Android Development. It combines the features of Object-Oriented Programming (OOPs) and functional programming into a unique platform.

Features of Kotlin:

Concise: Kotlin is the language where lines of code can be reduced up to 40%. This makes Kotlin an ideal choice for software development.

Null Safety: Kotlin aimed to eliminate the NullPointerException (null reference) from the code. Kotlin system can easily distinguish between null references and not null references. For example:

var name:String="Aman"
name=null //This is will give an error 
To permit null value we can declare it as:
var name:String?="Aman"
name=null
Enter fullscreen mode Exit fullscreen mode

Interoperable: Kotlin is fully compatible with Java Code which means existing Java code can be naturally called from Kotlin, and Kotlin code can be used from Java.
Fast Compilation: It is easy to compile the Kotlin code which results in better performance and fast compilation time.
Smart cast: The Kotlin compiler automatically converts the variable to a particular class reference once it's passed through any conditional operator.
Extension function: Kotlin supports a variety of extension function which means it helps to extend the functionality of classes without touching their code.

Variables:

Variables are the name we give to computer memory location. These are used to store and manipulate the data. We can declare a variable by using var and val keyword.

var name="aman" //string
val age=10 //integer
Enter fullscreen mode Exit fullscreen mode

We can also explicitly specify the datatype of the variable:

var name:String="aman" //string
val age:Int=10 //integer
Enter fullscreen mode Exit fullscreen mode

var: We can change the value of the variable declared using var keyword. Therefore it is known as a mutable variable. For example:

var age:Int=8
age=10
print(age)
Enter fullscreen mode Exit fullscreen mode

This will give 10 as output

val: We can not change the value of the variable declared using val keyword. Therefore it is known as an immutable variable. For example:

val age:Int=8
age=10
print(age)
This will give an error(Val cannot be reassigned)
Enter fullscreen mode Exit fullscreen mode

Data Types in Kotlin:

Data types are set of relatable values and describe the operations that can be operated on them. It refers to the type and size of data associated with variables and functions. In Kotlin every data type is considered as an object.
Kotlin has the following categories of data type:
Number: 
This data type holds only number type data variable. It is further sub-categorised as Integers and Floating-Point Numbers.

  • Integers: It contains numeric value without decimal. It has four types:

image

var byteVar:Byte=29 //byte data type
var shortVar:Short=323 // short data type
var intVar:Int=40000 //int data type
var longVar:Long=8673463 // long data type
Enter fullscreen mode Exit fullscreen mode
  • Floating point: It contains non Integer numbers that carry some decimal values. It has two types- Float which has a 32-bit single-precision floating-point value and Double which has a 64-bit single-precision floating-point value.
var floatVar:Float=29.0f // float data type
var doubleVar:Double=8450.445 //double data type
Enter fullscreen mode Exit fullscreen mode

Boolean:
It represents the logical value. It contains either true or false.
image

var boolVar:Boolean=true //boolean data type
Enter fullscreen mode Exit fullscreen mode

Char:
Character in Kotlin is represented with the help of char keyword. Character can be declared using a single quote 'c'.
image

var charVar:Char='c' //char data type
Enter fullscreen mode Exit fullscreen mode

String:
Strings are the arrays of character means a string will contain one or more than one characters. Strings in Kotlin are immutable, which means we cannot change the elements in String. Kotlin has two kinds of string raw string and escaped string. Escaped String allows providing extra line space after the first print statement.

var escapedString:String="first name \n" //escapedString
var rawString:String="last name" //rawString
Enter fullscreen mode Exit fullscreen mode

Arrays:
Arrays are used to store homogenous data. We can create arrays of different data types like an array to store int value and another array to store string value. Arrays are represented by Array class. We can create array using library function arrayOf() and Array() constructor.

Differents approach to declare array
val stringArray=arrayOf("name","age")
val stringArray=arrayOf<String>("name","age")
val stringArray=Array(2){"name";"age"}
val stringArray=Array<String>(2){"name";"age"}
Enter fullscreen mode Exit fullscreen mode

We can access elements of an array using the index. For example, if we want to access the first element then we can access it with 0 index.

val stringArray=Array<String>(3){"first";"second";"third";}
print(stringArray[1])
This will third as output
Enter fullscreen mode Exit fullscreen mode

Array index starts from 0. So if we are accessing the first element then we have to use the index 0.

That's it for this article. We will continue in the next article.
Happy Learning!!

Top comments (0)