Skip to main content

Posts

Showing posts with the label Kotlin

Kotlin - Control Flow - when

Greetings! Just like if, when also an expression. It has 2 forms. With a value - behave as a switch operator. Without a value - behave as if-else-if chain. when as a switch Java private void dayOfWeek(int dayOfWeek) { switch (dayOfWeek) { case 1: System.out.println("Sunday"); break; case 2: System.out.println("Monday"); break; case 3: System.out.println("Tuesday"); break; case 4: System.out.println("Wednesday"); break; case 5: System.out.println("Thursday"); break; case 6: System.out.println("Friday"); break; case 7: System.out.println("Saturday"); break; default: System.out.println("Invalid day"); } } Kotlin private fun dayOfWeek(dayOfWeek: I...

Kotiln - Control Flow - If

Greetings! If is the most basic way to control flow in Kotlin. Unlike Java, in Kotlin if is an expression. That is it return a value. Statement - is a program instruction that return no value. Can't be on right side of the equal sign. Expression - is a program instruction that return values. Can be assign to a variable. Java private void findMax(int a, int b) { int max; if (a > b) { max = a; } else { max = b; } System.out.println("Max value is " + max); } Kotlin (traditional statement) private fun findMax(a: Int, b: Int) { val max: Int if (a > b) { max = a } else { max = b } println("Max value is $max") } Kotlin (as an expression) private fun findMax(a: Int, b: Int) { val max: Int = if (a > b) { a } else { b } println("Max value is $max") } Ternary Operator Kotlin doesn't have a ternary operator. It's bec...

Kotlin - Variables and Type Inference

Greetings! Unlike Java, Kotlin uses special keywords to declare variables. var - for the values that change, mutable. val - for the values that do not change, immutable. Java String name = "Java"; int age = 20; Kotlin val name = "Kotlin" val age = 4 It is best practice to use val because immutability guarantees safety. We can use variable type when we declare it. If we initialize it, we can remove the type. But if we do not initialize, we should declare the variable type. val name = "Jon Snow" val role: String role = "King in the north" Constant If the value is a truly constant we can const keyword when declaring. Though this needs to use companion object. const val THE_END = "All men must die" Type inference Kotlin is a strongly typed language. Compiler can deduce the type by the context eliminating boilerplate code. Compiler is smart enough to identify the variable type. val helpUs = "Save Wilpattu...

Kotlin - Introduction

Greetings! I'm going to talk about Kotlin as a Java developer. There are many languages looming and Kotlin is one of them. Since it is another JVM based language it will be easier to grasp. Why another language? Java is more than 20 years old mature, widely used language. The problem of being old is it lacks modern techniques. Even though Java adapted to functional programming with Java 8, I believe it is somewhat late for the game. Considering modern day language features, difficulties they have faced using Java, Jetbrains created Kotlin. Being the one of the best IDE providers they know what they are doing. With InteliJ Idea Kotlin has the best IDE support with it. What is Kotlin Kotlin is a cross-platform, statically typed, general-purpose programming language with type inference. Kotlin is designed to interoperate fully with Java, and the JVM version of its standard library depends on the Java Class Library, but type inference allows its syntax to be more concise. ...