KANNIKA LIBRETA
  • Home
  • Kotlin & Spring Boot
Picture

Kotlin

Spring Boot

Picture
Picture

Kotlin

Intro

Kotlin is compatible with Java and it is used for developing Android applications, web development, and server-side applications.

Basics

  • Main.kt
fun main() {
  println("Hello World")
  var name = "Kannika"
  val birthyear = 1999    // Note:
 Val cannot be reassigned, it will generate an error

  print(name)          // Prints the value of name
  print(birthyear)     // and the value of birthyear on the same line
}
Variables, When, Arrays
Variables
/* This will generate an error 
var name
name = "Kannika"
println(name)  */


/* This works fine */
var name: String
name = "Kannika"
println(name)

var txt = "Hello World"
println(txt[0]) // first element (H)
println(txt[4]) // third element (o)

var firstName = "Kannika"
var lastName = "Kabilar"
println("My name is $firstName $lastName")

// Like Switch => When
val day = 4

val result = when (day) {
  1 -> "Monday"
  2 -> "Tuesday"
  3 -> "Wednesday"
  4 -> "Thursday"
  5 -> "Friday"
  6 -> "Saturday"
  7 -> "Sunday"
  else -> "Invalid day."
}
println(result)
// Outputs "Thursday" (day 4)

// Arrays
val cars = arrayOf("Volvo", "BMW", "Ford", "Mazda")
if ("Volvo" in cars) {
  cars[0] = "Lambo"
  println("Volvo changed to Lambo!")
} else {
  println("It does not exist.")
}
println("There are " + cars.size + " cars in the array") // Outputs 4

// Note!: No traditional for loops in Kotlin (ie: int i = 0; i < n; i++)
for (x in cars) {
  println(x)
}

for (chars in 'a'..'x') {
  println(chars)    // prints [a to x]
}
Functions

fun myAdd(x: Int, y: Int): Int {
  return (x + y)
}

fun myAdd2(x: Int, y: Int) = x + y


fun main() {
  var result = myAdd(3, 5)
  println(result)    // 8
  
println(myAdd2(3, 5))    // 8
}

Classes - Kotlin is OOP

// Example 1​
class Car(var brand: String, var model: String, var year: Int) {    // This line acts as a constructor
  // Class function
  fun drive() {
    println("Wrooom!")
  }

  // Class function with parameters
  fun speed(maxSpeed: Int) {
    println("Max speed is: " + maxSpeed)
  }
}

fun main() {
  val c1 = Car("Ford", "Mustang", 1969)
  
  // Print property values
  println(c1.brand + " " + c1.model + " " + c1.year)
  
  // Call the functions
  c1.drive()
  c1.speed(200)
}

// Example 2: Inheritance
// Superclass
open class MyParentClass {
  val x = 5
}

// Subclass
class MyChildClass: MyParentClass() {
  fun myFunction() {
    println(x) // x is now inherited from the superclass
  }
}

// Create an object of MyChildClass and call myFunction
fun main() {
  val myObj = MyChildClass()
  myObj.myFunction()
} 

Picture

Spring Boot

Intro

Spring Boot helps to create minimum configuration applications that you can just run. Utilizes a Java based framework for creating microservices. 

Micro Service is an architecture that allows the developers to develop and deploy services (smaller sub-parts of an application) independently. 

Create a free web site with Weebly
  • Home
  • Kotlin & Spring Boot