A basic Kotlin Program using IntelliJ – Kotlin Fundamentals


Hey Guys, Welcome back. In this video, we’ll write and run a basic
Kotlin program. So go ahead and launch IntelliJ. Whenever you launch IntelliJ, you will see
this welcome screen. Now click “Create new project”. Whenever we start writing a new Kotlin program
in IntelliJ, we need to create a new project for it. Now we need to decide, what kind of project
we want to create, and we also need to decide which platform we want to target, so chose
JVM. Now click on the “next” button. Now give your project a nice name,
and accept defaults for the rest. Then click on the “finish” button. Now, as you can see, the IDE has created a
new project for us, and we can use this file explorer to navigate files and folders of
this project. This folder named “src” is going include
the source files of our project. And we’ll spend most of our time working
with this folder. Now in order to write Kotlin code, we need
to create a Kotlin file. So right-click the source folder, then chose
“new”, now click “Kotlin file slash class”. Name it whatever you want, then click “OK”. Now, as you can see, the IDE has created a
new Kotlin file in the source folder. Now in order to minimize the project explorer,
you can double-click here. Whenever you write a program, you should always
comment your code, because other programmers and humans are going
to read and maintain your code in the future, so you should clearly explain what your code
is supposed to do. You can do so by using the comments. A comment is something that is completely
ignored by the compiler and its only readable to humans and programmers. In Kotlin, a single-line comment is followed
by two slashes and it continues until the end of the line. And a multi-line comment starts with a slash
followed by an asterisk and ends with an asterisk followed by a slash. Now we’re going to write some code that
will display a sequence of characters on the screen. Whenever we write a Kotlin program, we must
add a “main” function. Because when we compile and execute a Kotlin
program, the compiler looks for the main function. Now in order to write the main function type
the keyword “fun”. In Kotlin, every function begins with the
keyword “fun”. Now type the function’s name followed by
parenthesis. Now inside the parenthesis, we are going to
specify that this function accepts an array of strings as an argument. Don’t worry if you don’t understand what
an array and function are all about, we will learn everything in detail in later videos. For now, the only thing you should be concerned
about is that the main function is the entry point of every Kotlin program. You can think of a function as a group of
statements that collectively has a name. Now, if you’re using Kotlin 1.3 or later,
you can safely omit these parameters. Now, add an opening and a closing curly brace. Now, this line is the function’s header,
and everything between the opening and the closing curly brace is the function’s body. Now we need to call the “println()” function
and pass a sequence of characters inside quotation marks as an argument. In programming, a sequence of characters in
quotation marks is called a string. Now press the green button and our IDE will
compile and execute our Kotlin program. Whenever you press the “run” button, IntelliJ
will execute everything in the function’s body, one line at a time. Now, as you can see, our program is displaying
the string on the console. So in this video, we wrote a basic Kotlin
program. And we learned that the main function is the
entry-point of every Kotlin program. In the next video, we will learn about Kotlin
REPL. Thank for watching and I will see you in the
next video.

Leave a Reply

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