Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Basic Syntax and Structure of Kotlin | Kotlin Basics
Android Development with Kotlin
course content

Зміст курсу

Android Development with Kotlin

Android Development with Kotlin

1. Kotlin Basics
2. Control Flow and Collections
3. Object-Oriented Programming in Kotlin

Basic Syntax and Structure of Kotlin

Let's start learning Kotlin from the very basics.

Hello World Program

When learning any language begins, it's customary to see how the most standard program, Hello World, is written. This program should print this phrase to the console, letting the world know that we've begun learning Kotlin and will soon reach new heights!

Let's see how this code looks in Kotlin and break it down:

kt

main

copy
123
fun main() { println("Hello World!") }

Let's break down this code step by step.

We start with fun main(). Unfortunately, in this case, "fun" doesn't mean fun as in enjoyment but rather denotes a function. Kotlin is entirely built around functions, and throughout this section, we'll explore what they are and how to create our own functions.

The main function is the entry point of the application and will execute whatever is written inside it.

Next, we see the opening of the function body. Kotlin is a structured language, so all code is written within curly braces {}, which denotes the opening and closing of code blocks.

For example:

This makes it easy to structure your code.

Following that, there's the console output function: println(), which stands for the "print line". This function takes some text inside parentheses and prints it to the console. The text should be enclosed in double-quotes, as you see in the code.

When you click "Run Code," you'll see that the text inside the println function is displayed on the console.

You can change the text inside the println function yourself, for example, to "Hello Codefinity", and see how the text output on the console changes.

It’s worth noting that Kotlin has two functions for printing text to the console. The println function, which we are already familiar with, prints text to the console and adds a newline character to move to the next line.

On the other hand, the print function prints text to the console without moving to a new line.

Let's look at the following example to see the difference and understand how it works:

kt

main

copy
12345
fun main() { println("After this line, there will be a newline") print("After this line, there will be no newline ") println("This line will be on the same line as the previous one") }

As you can see, the text automatically moves to the next line after using the println function, while after using the print function, it remains on the same line as before.

Variables

Let's start with what a variable is in general.

We can access the value of the variable at any time by using the name we gave to the variable.

The syntax for declaring a variable looks like this:

We need to use the keyword val or var to declare a variable, after which we give our variable a name and the value it will store. Creating a variable is simple, as is using it.

But first, let's clear something up. You might be wondering:

"What is the difference between val and var?"

Let's figure that out!

val vs var

Let's get straight to the point:

  • val is used to declare a variable whose value cannot be changed in the future;
  • var is used to declare a variable whose value can be changed in the future.

Let's look at an example:

kt

main

copy
1234567
fun main() { val immutableValue = 10 // This is an immutable variable var mutableValue = 20 // This is a mutable variable immutableValue = 15 // Error: trying to change the value of an immutable variable mutableValue = 25 // This is correct, the value of a mutable variable can be changed }

As you can see, we have two types of variables in the code above: immutable and mutable. They hold integer values, 10 and 20, respectively.

Next, we try to change the value of the immutableValue variable, which causes Kotlin to throw an error, indicating that we cannot change the value of a variable declared with val.

From this code example, you can also see how we can change the values of variables. We simply need to write the variable name and assign it a new value using the equal sign.

Note

We can only do this with mutable variables(declared with the var keyword).

It is also worth noting that the variables we declared contain integer values. In the next chapter, we will explore data types and learn about the various types of data and how to create them. Stay tuned!

1. What keyword is used in Kotlin to declare a variable whose value cannot be changed?
2. Which function prints text to the console and moves to a new line in Kotlin?

What keyword is used in Kotlin to declare a variable whose value cannot be changed?

Виберіть правильну відповідь

Which function prints text to the console and moves to a new line in Kotlin?

Виберіть правильну відповідь

Все було зрозуміло?

Секція 1. Розділ 2
We're sorry to hear that something went wrong. What happened?
some-alt