The Core Syntax of Kotlin – Part 1

The Core Syntax of Kotlin – Part 1

If you are new to programming, this series may not be the best entry point. In this case, check out my youtube channel, where I am starting a series covering the core fundamentals of programming using Kotlin. This series however, is designed for programmers with the basic knowledge of the core principles of programming, which apply to most programming languages. Make sure to download IntelliJ if you haven’t already and create a new file called “main.kt”.

This is the entry point of our application:

fun main(args: Array){

}

Inside of Kotlin, we declare functions using the fun keyword. Printing out information is easy as well:

fun main(args: Array){

   println("Followed by new line")
   print("Not Followed by new line")
}

Let’s now get to declaring variables:

fun main(args: Array){

   var age: Int = 10
   age++
   val yearOfBirth = 1990
   // Changing yearOfBirth would give an error
}

The major datatypes we use are:

– String
– Int
– Double
– Float
– Char
– Boolean

There are others like Short, Byte or Long which you’ll be able to use as well.

We use the var keyword, followed up by a name, a : and the appropriate datatype to declare variables. We then initialize them with the appropriate value.

Note: Inside of Kotlin, specifying the datatype is optional. The compiler will be able to figure it out on it’s own.

With the val keyword we define constants, which are not allowed to change at runtime. For example, it would make sense to define PI inside of a constant.

 

This is how we can define an array of variables:

fun main(args: Array){

   var allNames = arrayOf("Tim", "Bob", "Rob", "Kevin")
}

using the arrayOf() function allows you to declare your array. You can use a similar function, called listOf(), to declare lists.

Printing out all elements in our array is easy as well:

fun main(args: Array){

   var allNames = arrayOf("Tim", "Bob", "Rob", "Kevin")

   for (name in allNames){
      println(name)
   }
}

Similar to languages like python, we specify the name we want to use for the current elements and use the in keyword.
However, inside of Kotlin there is no way to use our well known for loop for printing out numbers until a specific value like in this this: (int i = 0; i < 10; i++). Instead, we use ranges.

fun main(args: Array){

   for (num in 1..10){
       print(num)
   }
}

You can also define your range in a variable if you prefer it:

fun main(args: Array){

   val oneToTen = 1..10

   for (num in oneToTen){
       print(num)
   }
}

There are also multiple ways of reversing the order:

1.

fun main(args: Array){

   val oneToTen = 1..10

   for (num in oneToTen.reversed()){
       print(num)
   }
}

2.

fun main(args: Array){

   val tenToOne = 10.downTo(1)

   for (num in tenToOne){
       print(num)
   }
}

You can either call the reversed() function, or simply use the downTo() function when initializing your range.

If-Else statements are basically the same as in most programming languages:

fun main(args: Array){

   val age = 18
   val isWithParents = true

   if (age >= 18){
      println("Welcome to this club!")
   }
   else if (age < 18 && !isWithParents){
      println("Sorry, you can't enter this club!")
   }
   else if (age < 18 && isWithParents){
      println("Welcome to this club!")
   }
}

These are the most basics of the basics you will find. In the next post, we will continue this series and add on our syntax scanning. We will be covering things like when, which is what you might know as switch, functions, classes and more.

See you on the next post.

Leave a Reply

Your email address will not be published. Required fields are marked *