Lynda-1

**********************************************************************

Type system - Here Type system means type of data of variable. see below

1. Kotlin automatically provide data type implicitly when passing value

Code - 
            var number = 5
            val sString =  "This is message."

2.  If not provide value then must specify data type

Code - 
            var number : Int
            val sString : String

Example
-----------------------------------------------------------
    var myChar = 'A'
    var myByte : Byte = 23
    var myShort : Short = 127
    var myInt = 2564
    var myLong = 2564L
    var myFloat = 2.36F // or f
----------------------------------------------------------

Readability - Underscore( _ ) can be use for readability in number

--------------------------------------------------------

val aLongNumber = 123_456_789 // == 123456789

--------------------------------------------------------

UpperCasting - UpperCasting not possible automatically, must convert value

------------------------------------------------------------------------

var aInt : Int = 1254
val aLongNumber : Long = aInt.toLong()

-----------------------------------------------------------------------

*******************************************************************************

Control Flow - Take decision to run this block or not.

Some Point
1. If is an expression not a statement

Statement - Program instructions that returns no value; can't be right side of an equal sign.

Expression - Program instructions that returns values; can be right side of an equal sign.

If in kotlin like Condional Operator in java
------------------------
// int lowest = (myInt < aInt) ? myInt : aInt
val lowest = if( myInt < aInt ) myInt else aInt
-----------------------------
Can Also write like this - when more than one line
-------------------------------
val lowest = if( myInt < aInt ) {
print("Value of myInt $myInt")
myInt
}  else {
print("Value of myInt $aInt")
aInt
}
  -----------------
2. When 'IF' use as an expression, must contain 'ELSE' cases

3. Case must have value in all case
--------------------
WRONG  -  [ val lowest = if( myInt < aInt )  else aInt ]
---------------------
4.

-> void (java) == unit (kotlin)

***********************************************

***********************************************
When - Kotlin replaces old switch statement with when. Some point

1. It has two form - With argument and without

With Argument
----------------------------------------------------------
 val isHungry = 1
 
    when(isHungry){
        0 -> print("Not Hungry")
        1 -> print("Hungry")
        2 -> print("Very Hungry")
        else -> {
            print("Not know")
        }
    }
---------------------------------------------------------

With Argument - Combine condition
----------------------------------------------------------
 val isHungry = 2
 
    when(isHungry){
        0 -> print("Not Hungry")
        1,2 -> print("Hungry")
        3 -> print("Very Hungry")
        else -> {
            print("Not know")
        }
    }
---------------------------------------------------------

When not limited to compare like switch
-----------------------------------------------------------
val isBurgerOrdered = -2

    when(isBurgerOrdered){

        Math.abs(isBurgerOrdered) -> print("Yupp!, Ordered")

        else -> {
            print("Less than 0")
        }
    }
----------------------------------------------------------

When using Range - in operator use to check in range

-----------------------------------------------------------
    val isBurgerOrdered = 8

    when(isBurgerOrdered){

        0 -> print("We need order")
        in 1..4 -> print("Some order done")
        in 5..9 -> print("Business Up")
        else -> {
            print("Less than 0")
        }
    }
----------------------------------------------------------


When with NO ARGUMENT


-----------------------------------------------------------
    val isBurgerOrdered = 8

    when(isBurgerOrdered){

        0 -> print("We need order")
        in 1..4 -> print("Some order done")
        in 5..9 -> print("Business Up")
        else -> {
            print("Less than 0")
        }
    }
----------------------------------------------------------

    val isBurgerOrdered = 8

    when{

        isBurgerOrdered <= 0 -> print("None order")
        isBurgerOrdered % 2 == 0 -> print("Even number order")
        isBurgerOrdered % 2 == 1 -> print("Even number order")

    }

----------------------------------------------------------
***********************************************
*************************************************
LOOP

While & Do..While

While - Repeat the block until condition get false

------------------------------------------------------------------

    var x = 0

    while (x < 10){
        println("Value is - $x ")
        x += 3
    }


-----------------------------------------------------------------

Do...While - Execute code block first and then check condition

------------------------------------------------------------------

       var x = 0

    do{
        println("Value is - $x ")
        x += 3

    }while (x < 10)


-----------------------------------------------------------------

for

-> Iterate number from range
--------------------------------------------------
    for (item in 1..10){
        println("Value is $item")
    }
--------------------------------------------------

-> Iterate char from String
--------------------------------------------------
    for (ch in "MngoMan"){
        println(ch)
    }
--------------------------------------------------

*******************************************************
*******************************************************

Advance Loop

Step
---------------------------------------------------------------------

    var ndx = 0
    for (item in 10.rangeTo(20).step(2)){
        println("${++ndx}) $item, ")
    }

---------------------------------------------------------------------

WithIndex
--------------------------------------------------------------------
    for ((index, item) in 10.rangeTo(20).withIndex()){
        println("${index + 1}) $item, ")
    }
-------------------------------------------------------------------

loop with array
--------------------------------------------------------------------

    var myArray = arrayOf(11, 22, 33, 44, 55)
    for ( index in myArray.indices ){
        println("${index}) ${myArray[index]}, ")
    }

-------------------------------------------------------------------

*******************************************************

Function (fun) -> 'fun' keyword is use to declare function


---------------------------------------------------------------------------
Calling Function

println("10 + 20 = ${sumValue(10, 20)}")

-------------------------------------------------------------

fun sumValue(param1 : Int, param2 : Int) : Int{

    return param1 + param2
}
---------------------------------------------------------------------------


Syntax

---------------****************--------------------------------
1. Simple

fun functionName()
{  // Do Some Task Here }

2. With single Parameter

fun functionName(param1 : Data-Type)
{  // Do Some Task Here }

3. With multiple Parameter

fun functionName(param1 : Data-Type, param2 : Data-Type)
{  // Do Some Task Here }

4. With multiple Parameter and return type

fun functionName(param1 : Data-Type, param2 : Data-Type)  :  Return Type
{  // Do Some Task Here and return value
     return param1 + param2
}

5. With no return type

fun functionName(param1 : Data-Type)  : Unit
{  // Do Some Task Here }

5. With expression

fun functionName(param1 : Data-Type, param2 : Data-Type)  :  Return Type = param1 + param2

Ex -
-------------------------------------------------------------

fun sumValue(param1 : Int, param2 : Int) : Int = param1 + param2

---------------------------------------------------------------------------

6. With expression2

If parameter datatype and return data type is similar then no need return type

fun functionName(param1 : Data-Type1, param2 : Data-Type1)  = param1 + param2

Ex -
-------------------------------------------------------------

fun sumValue(param1 : Int, param2 : Int) = param1 + param2

---------------------------------------------------------------------------


7. Default Parameter


Iparameter datatype and return data type is similar then no need return type

fun functionName(param1 : Data-Type1 = default_value)

Ex -
-------------------------------------------------------------

fun sumValue(param1 : Int = 4, param2 : Int = 5, message : String = "Hi") : Int{

    val results = param1 + param2
    println("$message")
    return results
}

Note : Here, 4, 5 and "Hi" is default value.
---------------------------------------------------------------------------

8. calling function with named parameter

functionName(param1 : "Hello Test")

fun functionName(param1 : Data-Type1)  = param1 + param2

Ex - 
-------------------------------------------------------------

println("10 + 20 but default (4, 5, \"Hi\") = ${sumValue(message = "Test")}")

-------------------
fun sumValue(param1 : Int = 4, param2 : Int = 5, message : String = "Hi") : Int{

    val results = param1 + param2
    println("$message")
    return results
}

---------------------------------------------------------------------------

---------------_****************--------------------------------

*******************************************************

Packages

Packages are optional but you can declare

Kotlin by default import following packages

----------------------------------------

- kotlin.*
- kotlin.annotations.*
- kotlin.collections.*
- kotlin.comparisions.*
- kotlin.io.*
- ranges, sequences, text, lang, JVM

---------------------------------------














22

Comments

Popular Posts