reCAPTCHA WAF Session Token
Programming Languages

Your First Steps in Kotlin

Writing Android apps entails having a deep understanding of the Kotlin programming language. For the total beginner, this may appear to be a bridge too far. Programming languages can seem scary. They combine the high school anxiety of math with the seemingly mystic scribbling of shorthand.

To make matters worse, films and television shows often depict code as endlessly complex, with lines and lines of strange symbols that, often enough, have little to do with the plot of the story.

The good news is that code is not hard to learn. In fact, good code reads like words on paper and is quite easy to understand. It just takes a good teacher and a well-thought-out curriculum.

Kodeco has the best teachers, and an ideal curriculum for getting started with Kotlin, called Kotlin Essentials. In this program, you’ll learn how to read and write code from the ground up! You’ll do so in a friendly, supportive environment that makes no assumptions about your past experience. Don’t take my word for it; here’s a sample of this program to get you started with Kotlin, written by Godfred Afful.

Learning About Kotlin Playground

As you progress through this module, you’ll use the Kotlin Playground to master the basics of Kotlin. Among the numerous options displayed earlier, Kotlin Playground is the simplest and swiftest way to get started with Kotlin.

To launch a new playground session, visit play.kotlinlang.org. This interface is pretty straightforward and has fewer features than a comprehensive IDE such as Android Studio.

Running Kotlin Code

You’ll use the Run button to execute code in the Kotlin Playground. Throughout this module, you’ll write code in the editor, execute it with the Run button, and monitor the output in the console beneath it.

A screenshot that shows the Kotlin playground with the run button highlighted

Kotlin Playground is designed exclusively for the Kotlin language and doesn’t provide functionalities for creating complete software applications. For that, you would need to use Android Studio.

The Main Function

In Kotlin, the main function is the entry point to every JVM-based program. This means that the Kotlin compiler will look for this function when your program starts to execute. The main function, fun main(), has a specific format. You must name it ‘main’, and it shouldn’t take any arguments like this:

fun main() {
  println("main function without arguments.")
}

Or, pass in an array of strings as its argument, like this:

fun main(args: Array<String>) {
  println("main function without arguments.")
}

The Kotlin compiler won’t recognize anything else, and your program will result in an error if you run it without the main function. Change the name of the function from main to mains and click the Run button to run the program. You’ll see an error in the console output:

A screenshot that shows the main function in the Kotlin playground

You can have other functions in your program, but there should be only one main function. Did you notice that? Kotlin main and any other functions are fun. Get it? Keep on learning Kotlin, and you too will consider it fun!

Creating Variables

A variable is a named storage location for data. You can store any type of data in a variable. In programming terms, you assign data to variables. Once you assign data to a variable, it holds the value of the assigned data. The syntax for declaring variables in Kotlin is straightforward.

Open a Kotlin Playground session in your browser. Go to play.kotlinlang.org and create a variable called day using the following code:

fun main() {
  var day = "Monday"
  println(day)
}

var is a keyword in Kotlin for defining variables. day is the name of the variable. Monday is the data contained in the variable.

Having done this, you can use day anywhere in your program where you want to imply Monday. Until day is assigned a different value, it remains Monday throughout your program.

You may initialize a variable without assigning data by introducing lateinit. Declare a lateinit above the main function and assign a value to it as before:

lateinit var day : String

fun main(args: Array<String>) {
  day = "Monday"
  println(day)
}

Note: Make sure you assign a value to the variable before using it. Otherwise, you’ll get a runtime error.

Naming Variables

Always choose clear names for your variables. It’s good practice to name your variables in a simple, self-explanatory manner. In the example above, you can see that the variable’s name gives an idea of the value it contains.

By convention, you should name your variables using the lower camel case format. They must begin with letters and include numbers afterward if desired. No other characters are allowed.

Updating Variables

After initially assigning a value to a variable, you have to omit the keyword if you want to update the variable. Otherwise, you’d be re-initializing a variable with the same name, and that’s not allowed. Return to the original example and add a second var, again named day:

fun main() {
  var day = "Monday"
  var day = "Tuesday" // Not allowed
  println(day)
}

This results in an error. Instead, omit the var keyword to update the value of day:

fun main() {
  var day = "Monday"
  day = "Tuesday" // OK
  println(day)
}

Where to Go From Here?

Congratulations on surviving your first introduction to Kotlin! As you can see, it’s not too hard. This article was just an introduction to the very beginnings of Kotlin. You can learn about this is much more depth, and many other topics in the Learn the Kotlin Language course. You’ll start by taking a look at the basics of Kotlin, like variables and operators, before moving on to control flow, functions and classes.

You can access this course today, as part of a Kodeco subscription, or as part of our Kotlin Essentials on-demand bootcamp. This program of work will take you from zero to proficient in Kotlin, in your own time. Coupling learning materials with expert advice from our mentors, and real-world projects to test out your new-found Kotlin knowledge, this is the best way to learn Kotlin from the ground up.

While learning Kotlin is the first step in creating Android apps, the Kotlin language also provides many other opportunities. Did you know you can actually use Kotlin to write iOS apps with Compose Multiplatform? You can also use Kotlin to write server applications using the Ktor framework. You can even write desktop apps for Windows, macOS, and Linux. The sky is the limit!

Learning Kotlin is an excellent investment in your career as a software developer, so sign up for Kotlin Essentials today!

Leave a Reply

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

Back to top button
WP Twitter Auto Publish Powered By : XYZScripts.com
SiteLock