0% found this document useful (0 votes)
26 views7 pages

Mad Kotline

The document discusses setting up the Kotlin environment and creating a simple Hello World program. It then covers Kotlin keywords, variables, data types, operators, control flows like if/when and loops. The document ends with functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views7 pages

Mad Kotline

The document discusses setting up the Kotlin environment and creating a simple Hello World program. It then covers Kotlin keywords, variables, data types, operators, control flows like if/when and loops. The document ends with functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Kotlin Fundamentals

Environment Setup

The following steps are required to complete to setup kotlin environment in your device.

• Download the compiler (https://github.com/JetBrains/kotlin/releases/tag/v1.8.10)


• Unzip it and place it in a computer drive.

First App

It is suggested to use notepad to type your kotlin program. The kotlin file should be saved using
extension.kt. The main function is required to start the execution.

fun main() {
println("Hello World")
}

Compilation and Execution

• Convert kotlin in to jar program.

kotlinc HelloWorld.kt -include-runtime -d HelloWorld.jar

• Run generated jar file

java -jar HelloWorld.jar


Keywords and Identifiers

Keywords
break do in
continue for var
val while this
null throw when
try true false
Identifiers
num x2 x_c
q5 test NUM

Variables and Constants

• Declare immutable variable.


val name = "MAD"
• Declare mutable variable.
var name = "MAD"
• Declare constant variable.
const val name = "MAD"

Kotlin Data Types

Type Size Minimum Maximum

Byte 1 byte -128 127


Short 2 bytes -32768 32767
Int 4 bytes -2,147,483,648 2,147,483,647
Long 8 bytes -9,223,372,036,854,775,808 9,223,372,036,854,775,808

Float 4 bytes 1.40129846432481707e-45 3.40282346638528860e+38


Double 8 bytes 4.94065645841246544e-324 1.79769313486231570e+308
Char 16 bits ‘\u0000’ (0) ‘\uFFFF’ (65535)

Boolean 1 bit true or false

Input and output

• var data= readLine()


• print(“Hello”)

Kotlin Arrays and Strings

• Creating an Arrays
val x = arrayOf(90,15,60,10) // type of array is not fixed
val x = arrayOf(‘A’,15,2.5f)
val x = arrayOf<Int>(90,15,60,10) // type of array is fixed

• Creating a String
val info: String = "NIBM"

Attributes
length Represents the length of string
lastIndex Represents the index of last character in string
Methods
first() Returns first character in the string
last(): Returns last character in the string
compareTo(string) compare strings equality
capitalize() Returns a copy of this string having its first letter
uppercased
toLowerCase() Returns a copy of this string converted to lower case
Data Operators

Arithmetic Operators
+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus x%y

Comparison Operators
< Greater Than x>y
> Less Than x<y
<= Less Than or Equal x<=y
>= Greater Than or Equal x>=y
== Equal x==y
!= Not Equal x!=y

Assignment Operators
= Assignment x=10
+= Add and Assign x+=10
-= Subtract and Assign x-=10
*= Multiply and Assign x/=10
/= Divide and Assign x/=10
%= Modulus and Assign x%=10

Unary Operators
-- Decrement x--, --x
++ Increment x++, ++x
Logical Operators
&& And x<a && x>b
|| Or x<a || x>b
! Not !x

Selections

Syntax if:

if(x>10)
{
}
else if(x>5)
{
}
else
{
}

Syntax when:

val x: Int = 10

when(x) {
10 -> println("A")
20-> println("B")
30 -> println("C")
40 -> println("D")
50 -> println("E")
else -> println("F")
}

Repetitions

Syntax while:

var counter = 1

while (counter <= 10){


println(counter)
counter ++
}
Syntax do while:

var counter = 1
do{
println(counter)
counter ++
}
while (counter <= 10)

Syntax for:

for (i in 1..3) {
println(i)
}

Syntax repeat:

repeat(4){
x ->
println("This statement will be printed 4 times!!")
println("Index is: $x")
}

Continue and Break

Syntax for continue:


for (x in 1..10){
if (x % 2 == 0)
continue
println("Number is $x")
}
Syntax for break:
for (x in 1..10){
if (x % 2 == 0)
break
println("Number is $x")
}
Functions

Syntax:

fun display(number: Int): Int


{
return number
}

display(50)

You might also like