Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Kotlin Programming

Day-2
27-11-2022
Outline
• Basic Program
• Variables
• Data types
• Functions
• Control Flow
• Conditional Expression
• When
Kotlin Basic Program
fun main() {
println("Hello, World!")
}

➢Kotlin code is usually defined in packages. Package


specification is optional: If you don't specify a package
in a source file, its content goes to the default package.

➢An entry point to a Kotlin application is the main


function. Since Kotlin 1.3, you can declare main without
any parameters. The return type is not specified, which
means that the function returns nothing.

➢println writes a line to the standard output. It is


imported implicitly. Also note that semicolons are
optional.
Kotlin Variables
To declare a variable in Kotlin, either var or val
keyword is used.
var language = "French"
language = "German"
Difference Between var and val
➢val (Immutable reference) - The variable declared
using val keyword cannot be changed once the
value is assigned. It is similar to final variable in
Java.
➢var (Mutable reference) - The variable declared
using var keyword can be changed later in the
program. It corresponds to regular Java variable.
Kotlin Basic Types
The built-in types in Kotlin can be categorized as:
➢Numbers
➢Characters
➢Booleans
➢Arrays
Numbers
Type Size (bits) Min value Max value

Byte 8 -128 127

Short 16 -32768 32767

Int 32 -2,147,483,648 (-231) 2,147,483,647 (231 - 1)

Long 64 - 9,223,372,036,854,775,807
9,223,372,036,854,775,808 (263 - 1)
(-263)

Type Size (bits) Significant bits Exponent bits Decimal digits

Float 32 24 8 6-7

Double 64 53 11 15-16
Characters
Characters are represented by the type Char. Character
literals go in single quotes: '1'.

Special characters start from an escaping backslash \. The


following escape sequences are supported:
\t – tab
\b – backspace
\n – new line (LF)
\r – carriage return (CR)
\' – single quotation mark
\" – double quotation mark
\\ – backslash
\$ – dollar sign
Strings and Booleans
Strings in Kotlin are represented by the type String.
Generally, a string value is a sequence of characters
in double quotes ("):
val str = "abcd 123“

Booleans:
The type Boolean represents boolean objects that
can have two values: true and false.
Kotlin Basic Output
Koltin Output
You can use println() and print() functions to send
output to the standard output (screen). Let's take an
example:
fun main( ) {
println("Kotlin is interesting.")
}
Difference Between println() and print()
• print() - prints string inside the quotes.
• println() - prints string inside the quotes similar like
print() function. Then the cursor moves
to the beginning of the next line.
Kotlin Basic Input
For other data types, you can use Scanner object

import java.util.Scanner
fun main( ) {
val reader = Scanner(System.`in`)
var integer:Int = reader.nextInt()
println("You entered: $integer")
}

To get Long use nextLong(),


To get Float use nextFloat(),
To get Double use nextDouble(),
To get Boolean use nextBoolean().
Functions
Kotlin functions are declared using the fun keyword:

fun method(x: Int): Int {


return 2 * x
}

Function usage
Functions are called using the standard approach:
fun main() {
val result = method(2)
println(“The result is : $result”)
}
NOTE: Here is a link to the KOTLIN STANDARD LIBRARY
Conditions and loops
If expression
In Kotlin, if is an expression: it returns a value.
Therefore, there is no ternary operator (condition ?
then : else) because ordinary if works fine in this role

if(expression)
statement1
else
Statement2
Conditions and loops
When expression
when defines a conditional expression with multiple
branches. It is similar to the switch statement in C-like
languages. Its simple form looks like this

when (x) {
1 -> print("x == 1")
2 -> print("x == 2")
else -> {
print("x is neither 1 nor 2")
}
}

You might also like