Hello everyone, Welcome to the first blog of A programmer's guide to Android Development. In this blog we will be briefly looking into the following concepts of Kotlin
Basics of Kotlin Programming
Variables, Constants and Data types in Kotlin
Operators in Kotlin
Basics of Kotlin
Consider the following code
package bhuvan.tutorials.kotlinbasics
fun main(){ //fun in this case stands for function and the main is the name of the function
print("Hello World")
}
Well you might be confused why have I directly started off the blog with the code snippet. Well as all of you are aware every programming language begins its journey with Hello World. Just like all the other programming languages have functions, headers/packages Kotlin also follows the same line. If we look into the following above typed code snippet we look into three major things
Packages: If you are aware of how Java works with package, then understanding package in Kotlin isn't a great deal. Even without the knowledge of Java, you see Packages are really simple. Just like how Java uses packages to organize classes. Kotlin also has packages and they work in the same way. But the only compulsion in the following case is in Kotlin you can have a file or a class without a package.
Functions: A function groups together a series of code statements that perform a task. The details of the implementation of the function are hidden from the caller.
To know more about packages, functions in Kotlin, you can click here
Now let's get back to our task if printing Hello World. Here we see that we were successfully able to run the Hello World program. But now let's try to run the same in different manner
Consider the code:
package bhuvan.tutorials.kotlinbasics
fun main(){
var myName="World"
print("Hello "+myName)
}
In this case, here we note that we used a variable say myName instead of using directly printing the word World. Did you see any advantage of using these, If yes superb. Well while writing long codes, we often tend to store information to be referenced and manipulated in a computer program. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that hold information. myName is a variable similar to any other variable out there in other programming language.
Now consider the following code:
package bhuvan.tutorials.kotlinbasics
fun main(){
val myName="World"
print("Hello "+myName)
}
Here we used val instead of 'var'. The major difference being the variables of type var can be overwritten whereas the variables of type 'val' cannot be overwritten. In general they act as constants.
Note that Kotlin will automatically know the type of variable that is being used in the following case.
package bhuvan.tutorials.kotlinbasics
fun main(){
var myName="Bhuvan" //Type: String
var myAge=20 //Type: integer
//Let's understand differemt types of integers
var myByte: Byte=13
var myShort: Short=125
var myInt: Int=12121212
val myLong: Long=212121212121212
}
Here note that different data type requires different amount of space. Similarly Kotlin has 5 major data types under them. To know more about Data types in Kotlin Click here
Note:
- A string can be considered as a array of char data types. If you want to assign a character with a character from a string you can do it by equating it to stringname[index number]
Example:
package bhuvan.tutorials.kotlinbasics
fun main(){
var myString="Hello World"
var mychar=myString[0]
print("First Character in "+myString+ " is "+mychar)
}
- Here in the above codes we made use of a lot of + operator and many a times it can be irritating as well to open and close the braces and write + symbols. Hence to simplify these, we can make use of $ symbol. Consider the code
package bhuvan.tutorials.kotlinbasics
fun main(){
var myString="Hello World"
var Strlen=myString.length
println("The String is $myString and the length is $Strlen")
print("The String is $myString and the length is ${myString.length}") //Another way to represent the same
}
Here stringname.length is used to determine the length of the string
Error occurring when we try to overwrite a "val" is a compile time error
Operators in Kotlin
Arithmetic Operators
Arithmetic operators in Kotlin work similarly to that of how they work in other programming languages. That is we go on to declare two variables say a,b and we go on performing operations in them. The arithmetic operators that are available are
+ (Addition operator : To add two numbers)
- (Subtraction operator : To subtract two numbers)
** * ** (Multiplication operator : To multiply two numbers)
/ (Division operator : To divide two numbers)
% (Modulo operator : To find the remainder when one number is divided by another)
++ (Increment operator : To increment the value by 1)
-- (Decrement operator : To decrement the value by 1)
Consider the following code snippet which contains the example of all the Arithmetic operations
package bhuvan.tutorials.kotlinbasics
fun main(){
var a=10
var b=20
var result=a+b
println(result)
result=a-b
println(result)
result=a*b
println(result)
result=a/b
println(result)
result=a%b
println(result)
}
Type Conversion
Consider the following code snippet
package bhuvan.tutorials.kotlinbasics
fun main(){
var a=10.5
var b=2
var result=(a/b)
println(result)
var result2=(a/b).toInt()
println(result2)
}
Hey, do you observe any difference in the output. Well, the first output is of type Double and the second output in this case is of type int. In this case, we were able to get an integer-type output, because of typecasting. The addition of toInt() type cast the variable of type Double to that of Integer.
Comparison operator
Comparison operators are used to comparing two values. The comparison operator returns a Boolean value i.e., either true or false
Consider the following code snippet
package bhuvan.tutorials.kotlinbasics
fun main(){
var a=10.5
//Comparison opearator
var isEqual = 5==3 //Checks the left and right hand side
println(isEqual)
var isEq = 5==5
println(isEq)
println("isEqual variable is "+isEqual)
println("isEqual variable is $isEqual")
}
The various comparison operators are
\== (Equal)
!= (not Equal)
< (Less Than)
\> (Greater than)
<= (Less than or equal to)
\>\= (Greater than or equal to)
Now consider this cool way of solving things
println("5 is greater than 3 : ${5>3}")
The above-given method is another way of handling comparison operators
Assignment Operator
Assignment operators are used to assigning values to variables.
The various assignment operators are
\= (Equal)
+= (Equivalent to x= x+3)
-= (Equivalent to x= x-3)
\= (Equivalent to x= x3)
/\= (Equivalent to x= x/3)
%= (Equivalent to x= x%3)
Code snippet displaying assignment operator
fun main(){
var a=10
var b=a //= operator
a+=10 //Equivalent to a= a+10
a-=10 //Equivalent to a= a-10
a*=10 //Equivalent to a= a*10
a/=10 //Equivalent to a= a/10
a%=10 //Equivalent to a= a%10
}
These are the programming examples showing the usage of Assignment operators
Thanks for being a part of the first blog. To access the next part and reach to the main part of the blog click below
Day 2 Blog is Yet to released