Kotlin for Beginners : Part 2

Functions
fun main(args: Array<String>) {
println(max(4, 5))
}fun max(a: Int, b: Int): Int {
return if (a > b) a else
b
}
The function declaration starts with the fun
keyword, followed by the function name: max
, in this case. It’s followed by the parameter list in parentheses. The return type comes after the parameter list, separated from it by a colon.
Note that in Kotlin, if
is an expression with a result value. It’s similar to a ternary operator in Java: (a > b) ? a : b
Variables
- val (from value) — Immutable reference. A variable declared with val can’t be reassigned after it’s initialized. It corresponds to a final variable in Java.
- var (from variable) — Mutable reference. The value of such a variable can be changed. This declaration corresponds to a regular (non-final) Java variable.
//val example
val message: String
if (canPerformOperation())
{
message = "Success"
} // ... perform the operation
else {
message = "Failed"
}//var example
var answer = 42
answer = 56
Even though the var
keyword allows a variable to change its value, its type is fixed. For example, this code doesn’t compile:
var answer = 42
answer = "no answer"
//Error Type Mismatch
You’re not restricted to simple variable names; you can use more complex expressions as well. All it takes is putting curly braces around the expression.
fun main(args: Array<String>) {
if (args.size > 0) {
println("Hello, ${args[0]}!")
}
}
Classes
As you no doubt know, the idea of a class is to encapsulate data and code that works on that data into a single entity. In Java, the data is stored in fields, which are usually private. If you need to let clients of the class access that data, you provide accessor methods: a getter and possibly a setter
/* Java */
public class Person {
private final String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
}/* Kotlin */
class Person(val name: String)
Note that the modifier public
disappeared during the conversion from Java to Kotlin. In Kotlin, public
is the default visibility, so you can omit it.
class Person(val name: String,val isMarried: Boolean)
fun main(args :Array<String>){
val person = Person("akshay",false)
println(person.name)
println(person.isMarried)
}//output
akshay
false
Kotlin’s name property is exposed to Java as a getter method called getName()
. For Boolean properties, a special rule for getter naming applies: if the property name starts with is
, no additional prefix for the getter is added. Thus, from Java, you call isMarried()
.
Custom Accessors
Suppose you declare a rectangle that can say whether it’s a square. You don’t need to store that information as a separate field, because you can check whether the height is equal to the width on the go:
class Rectangle(val height: Int, val width: Int) {
val isSquare: Boolean
get() {
return height == width
}
}
fun main(args: Array<String>) {
val rectangle = Rectangle(10, 10)
println(rectangle.isSquare)
}//output
true