Learn Kotlin – Class

Tags

Although Kotlin is statically typed language, it supports object-oriented programming like Java. Before explaining further let us dive into the basic syntax of the Kotlin class.

class Vehicle { // class definition
	// class body
}

So, defining a basic class is as simple as this and similar to Java.

Before diving further into the class syntax let us first discuss about the four access modifier available in Kotlin.

Continue reading

Learn Kotlin – Function

Tags

Merry Christmas Guys! The year 2018 is about to end. This have been a great year. Happy New Year 2019, May your new year be filled with Joy and Happiness. This will probably be the last post for this year 2018. Hope to see you all in the next year!

In today’s post i am gonna write about Function. If you have been programming in any of the language then you’ve been making Functions. Functions are the basic building blocks in programming. Let us discuss about the way to making Functions in the Kotlin.

First of all, let us see the basic syntax of the function in Kotlin.

fun getSum(a : Int, b: Int):Int {
	return (a+b)
}

The function above named getSum takes two parameter a of type Int and b of type Int which returns type Int.

Continue reading

Learn Kotlin – Control Flow

Tags

In this post we are going to learn about the control flow in Kotlin. Let us learn about how to make a decision, how to perform a task multiple times by iterating and how to get out of the loop, how to continue to next iteration. So, today we will learn all about changing the control flow of Kotlin program. 

If..Else

When we want to make a decision in program we use if and else. This is not different from other languages like Java. Even the syntax is similar to Java. 
However, if is the expression and not the keyword so it returns the value only whenever essential. Let us see the syntax for if..else.

fun main(args: Array<String>) {
	val a : Int = 100
	val b : Int = 200
	
	if(a > b) {
		println("a is greater than b")
	} else {
		println("b is greater than a")
	}


}

Or we can also write without bracket in a single line like this:

if(a > b) print("a is greater than b") else print("b is greater than a")

In the code above it checks for one condition, here first it checks whether a is greater than b, if not certainly b is greater. But what if both are of same value. We will need one more condition to be checked. So we have else if for checking more condition.

Continue reading

Learn Kotlin – Array, Collection and Range

Tags

In this post we will discuss about the Array, Collection and Range in Kotlin.

Array

In Kotlin, we can create array using two different ways. First one is by using arrayOf() function and next one is by using Array() constructor.

Using arrayOf() function

val accountInfo = arrayOf("Sudeep", 34000)

The accountInfo array is of type Array<Any>. It holds array with two different types of element String and Int, next there are functions like intArrayOf(), charArrayOf(), booleanArrayOf(), longArrayOf(), shortArrayOf(), byteArrayOf().

th two different types of element String and Int, next there are functions like intArrayOf(), charArrayOf(), booleanArrayOf(), longArrayOf(), shortArrayOf(), byteArrayOf().

For example using intArrayOf(3,4,5,6) make an array of type integer with the given numbers. If you try of push some other types of data into the int array then it will throw compile error. 

Continue reading

Learn Kotlin – Hello world and Basic Data Types

Tags

Since, Kotlin is already a official programming language for Android Apps development, I have been learning Kotlin lately. In this post, i will take you through basic hello world skeleton program to data types, variables in Kotlin.

Hello World Program for Kotlin

fun main(args: Array) {
    println("Hello World!")
}

main is the entry point in the kotlin program.

Continue reading