Basics of kotlin with easy explanation

Kotlin ”Hello world” program

fun main(args:Array<String>)
{
print(“Hello world”)
}
Output:
Hello world

For more details visit:introduction part

Kotlin Keywords

Keywords are the basic building blocks of the program.It can not use as a variable.

Some keywords are given below:

as,for,super,class,try,when,while,throw,package,null,val,var etc.

Kotlin Variables

Variable points to the memory location which is used to store the data.

Stored value/data in the variable can be used anywhere/anytime according to the condition.

Variable declaration:

It is  declared with the help of var and val.

Val language=”kotlin”

Var mobile=”7060512022”

Or

Var mobile:Int

mobile=”7060512022”

Note:We don’t need to specify the type of variables.

Difference between the val and var.

val: val is like a final keyword in Java that means once the value assigned that value can not be changed.

var:var is a general variable which means these values can be changed in the program. 

Kotlin Data types

Data types tells the compiler that what kind of data do you want to store into the memory.

Example: integer,float,double,string etc.

Kotlin built data types are categorized into different categories.

1.Number

2.Character

3.Boolean

4.Array

1.Number data type

Numbers are only store numeric data types only.

Some number data types are given Int,Byte,Float etc

Example:

fun main(args:Array<String>)
{
var number:Int=7060512022
print(“$number”)
}

Output:

7060512022

2.Character data type

To represent the character in kotlin we use the Char data type.

Example:

fun main(args:Array<String>)
{
Val name:Char
name= 's’

print(“$name”)
}

Output:

s

3.boolean data type

Boolean data type contains value either true or false.

Example:

fun main(args:Array<String>)
{
val flag=true
print(“$flag”)
}

Output

true

4.Array data type

Array is used to store multiple values into the single variable.

We use arrayOf( ) function to store multiple values.

Example:

fun main(args:Array<String>)
{
Val array=arrayOf(1,2,3,4,5,6,)
Val first=array[0]
print(“$first”)
}

Output:

1

Kotlin Type Conversion

Type conversion is a process in which one data type is converted into the other data types.

This type conversion is totally different from the java type conversion.

Let’s understand with the help of example:

In java,

int num1=10;

long  num2=num1;// It is valid in java.

Here num1 is type of integer will automatically convert into the long and assigned the value into the num2.

In kotlin,

val num1:Int=10

val num2:Long=num1 // Error type  mismatch

Here num1 is type of integer will  not automatically convert into the long.

The size of long is larger than int so it will not convert int to long.

For converting int to long,we need to use toLong( ) function.

val num1:Int=10

val num2:Long=num1.toLong( ) // Now it is valid

Some of the important helper function which is used  for the type conversion(from smaller data type to larger data type and from larger data type to a smaller data type).

1.toLong(  )

2.toInt( )

3.toByte( )

4.toShort( )

5.toChar( )

6.toFloat( )

7.toDouble( )

Note:There is no type conversion for the Boolean data types.

Kotlin Operators

An operator is a symbol which is used to perform certain mathematical and logical operations.

It is used to perform certain operation between/among the operands.

Example,addition(+),subtraction(-),multiplication(*),division(/).

List of kotlin operator

Arithmetic operator

Assignment operator

Unary operator

Logical operator 

1.Arithmetic operator

Arithmetic operators are used to perform mathematical operations such as addition,subtraction,multiplication etc.

Example:

fun main(args:Array<String>)
{
Var a=20
Var b=10
print(a+b)
println(a-b)
println(a*b)
}

Output

30

10

200

2.Assignment operator

Assignment operator(=) is used to assign value into the variable.It takes the value from right to left.

Example:

Val  number=10

Here 10 is assigned in the number variable.

Example with the help of program

fun main(args:Array<String>)
{
Val number=10
Val sum:Int
sum=number+5
print(“$sum”)
}

Output:

15

3. Unary operator

Unary operator only works with single operands.some unary operands are given -,+,++,– ,!etc.

Example

fun main(args:Array<String>)
{
Val a=10
Val b=20
Val flag=true
println(“++a=”+ ++a)
println(“++b=”+ ++b)
println(“!flag=”+!flag)
}

Output:

++a=11

++b=21

!flag=false

4.Logical operator

Logical operators are used to check condition between the operands.some logical operands are given below:

&&=return true if both conditions true

||=return true if anyone condition true

Example:

fun main(args:Array<String>)
{
val a=10
val b=20
val c=30
val result:Boolean
result=(a<b)&&(c>a)

println(“result=”+result)
println(“opposite of result=”+!result)
}

Output:

result=true

opposite of result=false

Kotlin Input/Output

Kotlin input/output means taking input from the device or by the user and display the output on the screen.

Kotlin output:

Output operation can be performed with the help of print( ) and println( ).

Example:

fun main(args:Array<String>)
{
println(“kotlin program”)
print(“Hello kotlin”)
}

Output:

Kotlin program

Hello kotlin

Difference between the print( ) and println( )

print( ):It is used to print value inside the double quotes(“ “) or single quotes(‘ ‘)

println( ):

It is used to print value inside the double quotes(“ “) or single quotes(‘ ‘) and cursor moves the next line from the beginning.

Kotlin Input:

Kotlin input means we will take the input from the user.

To read string in kotlin ,we can use readLine( )

Example:

fun main(args:Array<String>)
{
println(“Enter string: ”)
Val name=readLine( )
print(“You entered string:$name”)
}

Output:

Enter string:Suman kumar

You entered string:Suman kumar

If you want take other data types as an input then you will  have to use Scanner object. 

1.You need to import java Scanner class.

Import java.util.Scanner;

2.Create scanner object from this class.

Val name=Scanner(System.’in’)

Now with the help of name object we can take input.

Example:

Here we will take the integer value from the user.

Import java.util.Scanner;
fun main(args:Array<String>)
{
Val reader=Scanner(System.’in’)
println(“Enter your phone number”)
Val phone=reader.nextInt( )
println(“Your phone number is=”+phone)
}

Output:

Enter your phone number

7060512022

Your phone number is

7060512022

Here nextInt( ) is used to take integer value and into the variable.

Similar we  can use nextBoolean( ),nextFloat( ) for the boolean and float value respectively.

Kotlin comments

Comments are the symbol which is used to provide information about the line of code.

Comment  statements are not executed by the kotlin compiler with giving error in the program.

There are two types of comments.

1.Single line comment(//…..)

2.Multi-line comment(/*………*/)

1.Single line comment

It is used to comment  single line statement.

It is denoted by //.

Example

fun main(args:Array<String>)
{
//Hello world program in kotlin
print(“Hello world”) 
}

Output:

Hello world

2.Multi-line comment

It is used to comment multi line statement.

It is denoted by /*……..*/

Example:

fun main(args:Array<String>)
{
/* Hello world
 program in kotlin */

print(“Hello world”) 
}

Output:

Hello world

sumankumar80

Hello Friends,I have written and developed this website because you can learn technology easily ,prepare for the placement and also for the any type of examination . By :Suman Kumar

Leave a Reply

Your email address will not be published. Required fields are marked *