KOTLIN: Android Development

Kotlin is a general purpose, statically typed language, can be used on client and server(response request from client).

Kotlin is used for android devices, it is an example for programming language, was created on 2011, powerful and easy to use, java and kotlin have a special relationship because they are compatible with each other.

File extension for kotlin files is .kt.

fun main() { println(“hello world”)

}

Strings are sequence of characters

For example:

  •     val myString="KOTLIN" 
        println(myString[0])
        println(myString[1])
        println(myString.isEmpty())//false
        println(myString.length)//6
        println(myString.substring(2,4))//TL
    

Options and methods you get depends on the type of the varible(like strings, integers).

println("The string is &myString) //string interpolation // The string is KOTLIN $ replaces the variable with the content of that variable.

Condition: If and else

  •   fun main(){
      val score=80
      if(score>100){
      println("you passed")
      }
      else{
      println("you failed")
      }
      }
    

//Output: you failed

Collections

To hold a bunch of variables at once. For example: lists.

  •   fun main()
      {
      val names=listOf{"riya","shalini","rakshit"}
      println(names[2]) //rakshit
      names.add("rohit")
    
      }
    

By default, collections in kotlin are immutable–> We cannot add/or remove elements from of a list. But you can achieve that by using mutable keyword.

  •    fun main()
      {
      val names=mutablelistOf{"riya","shalini","rakshit"} 
      println(names[2]) //rakshit
      names.add("rohit")
      }
    

All the elements should be of same type if not then it will throw an error. You can explictly mention it like this:

  •    fun main()
      {
      val names=mutablelistOf<String>{"riya","shalini","rakshit"} //all the elements should be of same type
      println(names[2]) //rakshit
      names.add("rohit")
    
      }
    

For loops

To print each element in a list of names:

  •    fun main()
      {
      val names=mutablelistOf<String>{"riya","shalini","rakshit"} //all the elements should be of same type
      for( name in names){
      println(name)
      }
      }
    

On every cycle/iteration of a for loop, the variable name will take the value of a single element from the list in order.

//Output: riya
shalini
rakshit

Use of for loops: To do something for certain number of times.

  •    for(i in 1..5){
      println(i)
      }
    

//Output:

1
2
3
4
5

  •    for(i in 1 until 5){
      println(i)
      }
    

//Ouput:

1
2
3
4

Functions

Chunk of codes that can be used later. Advantages: Easier to read code, saves space, time and manpower. For example:

  •   fun helloFn(){
      println("hello")
      }
    
  •    fun main(){
      helloFn()
      }
    

//Output

hello

We can give some input to the function as a parameter.

For example:

  •    fun helloFn(name:String){
      println("hello, $name")
      }
    
  •    fun main(){
      helloFn(name: "Riya")
      }
    

//Output

hello Riya

We can set the visibility of a function as private so that they cant be accessed by other classes or files.

For example:

  •    private fun helloFn(name:String){
      println("hello, $name")
      }
    
  •    fun main(){
      helloFn(name: "Riya")
      }
    

NULL/nullability

Having null value, if the variable is allowed to have a null value then you have update the type to include a ? question mark.

Cant call any method on someting which is set to null so we have to check if the variable is not null. For example:

  •    fun main(){
      val name:String? =null
      if(name!=null){
      println(name.toUpperCae())
      }
    

//Output

Here we get a empty output

Another example:

  •    fun main(){
      val name:String? =hello
      if(name!=null){
      println(name.toUpperCae())
      }
    

//Output
HELLO

SHORTHAND for checking NULL

You can use a shorthand of the above if condition by writing name?.toUpperCase() : This means we will only call this method on this variable if it is not null.

val firstName: String="riya" // or val firstName=“riya”

This is type inference.

Built in types in kotlin: double for decimals, and boolean only has true or false.

Comments: Add // double slash to convert any code snippet to comments.

Operators

  •    val s1="hello"
      val s2="key"
      val add=s1+s2 //string concatenation
      println(add)
    

-to-be-continued-