Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 22, 2021 10:31 am GMT

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 ofKotlin:

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

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" //stringval age=10 //integer

We can also explicitly specify the datatype of the variable:

var name:String="aman" //stringval age:Int=10 //integer

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=8age=10print(age)

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=8age=10print(age)This will give an error(Val cannot be reassigned)

Data Types inKotlin:

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 typevar shortVar:Short=323 // short data typevar intVar:Int=40000 //int data typevar longVar:Long=8673463 // long data type
  • 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 typevar doubleVar:Double=8450.445 //double data type

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

var boolVar:Boolean=true //boolean data type

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

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 
" //escapedStringvar rawString:String="last name" //rawString

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 arrayval stringArray=arrayOf("name","age")val stringArray=arrayOf<String>("name","age")val stringArray=Array(2){"name";"age"}val stringArray=Array<String>(2){"name";"age"}

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

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!!


Original Link: https://dev.to/csj5483/basics-of-kotlin-part-1-jh

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To