Introduction = 10 println(“found–“+i) } } Output: 123456789 found–2

Introduction

Kotlin is a programming language
introduced by JetBrains.It is strongly statically typed language.It runs on
JVM. Google announced Kotlin is an official language for android development in
2017. It is an open source programming language that combines object-oriented
programming and functional features into a unique platform.

We Will Write a Custom Essay Specifically
For You For Only $13.90/page!


order now

 

Data Types

Data types available are:

 

Numbers

Following are
variable length datatype of numbers:

Double

64

Float

32

Long

64

Int

32

Short

16

Byte

8

 

fun main(args: Array) {   val a: Int = 200   val d: Double = 133.00   val f: Float = 120.00f   val l: Long = 1004564012   val s: Short = 5   val b: Byte =     }

 

Character

In
Kotlin character is represented using char. A character is declared
in a single quote like ‘a’. Variable are declared in two ways :”var” and
another “val”.

fun main(args: Array)
{

   val letter: Char    // defining a variable

   letter = ‘A’        // Assigning a value to it

}

String

Strings are character
arrays. They are immutable. There are two types of string: raw String and
escaped String.

 

Arrays

Arrays are a
collection of same type of data. It supports arrays of different data types.

fun main(args: Array)
{

   val numArray: IntArray = intArrayOf(12, 23, 34,
42, 51)

   println(numbers2)

}

Output:34

 

Collections

Collection is a part
of the data structure. There are two types of collection: immutable
collection (lists, maps and sets) & mutable collection (type
of it is editable).

Ranges

Ranges is unique
characteristic of Kotlin. It provides an operator that helps you iterate
through a range. It is implemented using rangeTo() .It uses
operator form as (..).

fun main(args: Array)
{

   val i:Int 
= 4

   for (j in 1..9)

   print(j) // prints “123456789”

  

   if (i in 1..10) { // equivalent of 1 )
{

   val a1:Int = 12

   val b1:Int = 24

   var maximum: Int

   if (a1 > b1) {

      maximum = a

   } else {

      maximum = b1

   }

   print(“Maximum of a1 or b1 is ” + maximum) 

}

Output:

Maximum of a1
or b1 is 5

 

When

when operator matches the
variable’s value against the branch conditions. If it satisfies the branch condition,
then it executes the statement inside that scope.

fun main(args: Array)
{

   val x:Int = 5

   when (x) {

      1 -> print(“x = = 1”)

      2 -> print(“x = = 2”)

     

      else -> {  

         print(“x is neither 1 nor 2”)

      }

   }

}

Output:

x is neither 1
nor 2

Compiler matches the
value of x with the given branches. If it does not match any of
the branches, then it will execute the else part.

For
loop

Loop can iterate
through any kind of data structure. The implementation and use of For loop is similar
to Java for loop.

 

fun main(args: Array)
{

   val items = listOf(13, 22, 34, 44)

   for (i in items) println(“values of the
array”+i)

}

Output:

values of the
array1

values of the
array2

values of the
array3

values of the
array4

 

While
loop and Do-While loop

While and Do-While
works same as in other programming languages. In case of Do-while loop the
condition will be tested at the end of the loop. In case of while loop
condition will be tested at the start of the loop.

fun main(args: Array)
{

   var x:Int = 0

   println(“Example of While Loop–“)

   while(x)
{

   var x:Int = 0

   do {

      x = x + 10

      println(“I am inside Do
block—“+x)

   } while(x )
{

   var x:Int = 1

   println(“The value of X is–“+ myfun(x))

}

fun myfun(x:Int):Int
{

   return 2+x;

}

Output

The value of X
is–3

 

Continue & Break: The “break” keyword
terminates the controller flow if some condition has failed and “continue” does
the opposite.

fun main(args: Array)
{

   [email protected] for(x in 1..10) { // appling the
custom label

      if(x = = 6) {

         println(“I am inside if block
with value”+x+”
— hence it will close the operation”)

         [email protected] //specifing the label

      } else {

         println(“I am inside else block
with value”+x)

         [email protected]

      }

   }

}

Output:

Example of Break
and Continue

I am inside
else block with value1

I am inside
else block with value2

I am inside
else block with value3

I am inside
else block with value4

I am inside
else block with value5

I am inside if
block with value6

 

The controller
continues the loop, until and unless the value of x is 6. Once
the value of x reaches 6, it starts executing the if block and
once the break statement is reached, the entire control flow terminates the
program execution.

 

A class is a
blueprint of an entity and object is its state. It includes both behavior and
state. Class declaration consists of two components: class header & class
body.

Class myclass {
// class Header

   // class Body

}

class myclass {

   // data member

   private var name: String = “Infosys”

  

   // member function

   fun printMessage() {

      print(“You are working in best
company named-“+name)

   }

}

fun main(args: Array)
{

   val myobject = myclass() // create obj
object of myClass class

   obj.printMessage()

}

Output:

You are working
in best company named:Infosys

 

 

References: