KotlinBasics

KotlinBasics

We learn Kotlin with example


TABLE OF CONTENTS

fun main(), Readline, Data Types, String Templates/Interpolation, Conditionals, CalculatorApp, Loops, FizzFuzz

  • let's learn the 'main' function

fun main(){
    print("hello world! ")
    println("Hi Oybek")
    //new line
    println("Hellooo")
}
//output
hello world! Hi Oybek
Hellooo
  1. What is the main() function ?

    An entry point of a Kotlin application is the main function

  2. What is the difference between print and println?

    The print statement prints everything inside it onto the screen. The println statement appends a newline at the end of the output.

  • Readline()

fun main(){
    print("What is your name?: ")

    val name = readLine()

    println(name)
}
//output:
//What is your name?: Oybek
//Your name is Oybek

What is the ReadLine() method?

Reads a line of input from the standard input stream.

  • Data Types

Kotlin has a different Data types:

  • Numbers

  • Characters

  • Booleans

  • Strings

  • Arrays

Number types are divided into two groups: Integer types and Floating point types

Integer types store whole numbers, positive or negative (such as 123 or -456), without decimals. Valid types are Byte, Short, Int and Long.

Floating point types represent numbers with a fractional part, containing one or more decimals. There are two types: Float and Double.

val myNum: Int = 5                // Int
var b:Float = 14.12f              //Float 
val myDoubleNum: Double = 5.99    // Double
val myLetter: Char = 'D'          // Char
val myBoolean: Boolean = true     // Boolean
val myText: String = "Hello"      // String

The Char data type is used to store a single character. A char value must be surrounded by single quotes, like 'A' or 'c':

val a: Char = 'B'
println(a)
//output
//B

The Boolean data type can only take the values true or false:

val n:Boolean = true
val b = false
println(n)
println(b)
//output:
//true
//false