Using Kotlin Interactively in REPL

Peter Sommerhoff
A free video tutorial from Peter Sommerhoff
Developer & Software Engineer - With 45,000+ Happy Students
4.5 instructor rating • 6 courses • 62,662 students

Lecture description

In this lecture, we use Kotlin interactively in the REPL (read-eval-print-loop) as a calculator, learn how to assign values and run some simple commands.

Learn more from the full course

Kotlin for Beginners: Learn Programming With Kotlin

Learn Kotlin from scratch! Grasp object-orientation and idiomatic Kotlin to realize coding projects and Android apps!

06:31:16 of on-demand video • Updated November 2020

  • Create professional applications using Kotlin, the new Java-based programming language developed by Jetbrains
  • Understand the concepts of the Kotlin language and how it integrates neatly with Java
  • Understand the basics of object-oriented software development, the most important development paradigm
  • Understand the principles behind other object-oriented languages like Java, C++, PHP, C#, Scala, or Swift
  • Use Intellij, the popular Java (and Kotlin) IDE, to write code effectively and professionally
  • Read code and write your Kotlin code as well
English All right guys. Now that we have IntelliJ up and running, we can go up here to tools, go to Kotlin and open up the Kotlin REPL. This stands for read-eval-print-loop and we can basically type in something over here and Kotlin is going to tell us right away what this evaluates to. For example, we can type in something like three plus four. Now we're going to have to click control enter, and it's going to tell us that is seven. Similarly, we can do multiplications by typing three times five with the star symbol and that's going to give us 15. To do something bit more interesting let's try to divide 7 by 4 and that's going to give us one and that's the case because we are doing integer division. This is going to evaluate to 1.75 you would expect, but everything behind the dot is basically cut off. It's not going to go around, it's not going to give you two, it's just going to cut off everything behind the dot. To get what you might have expected, you can tell Kotlin that this is not an integer but a float point number. You can do that by dividing 7.0 by four, press control shift and that's going to give you 1.75 as you would have expected, so that's something to be aware of. Next you can also put in text between double quotes like this and we can type in basically anything we want like Kotlin rocks and press control enter and that's going to give us the value which is basically just a text you put into quotes. What we can also do is we can concatenate text so we could say something like Udemy plus rocks. That's going to give us the text that is basically composed of these two individual texts. Also, Kotlin makes it pretty easy to get a list of something. You can simply write list off with a capital O. For example, can say a list of: one, two, three, four, five, press control enter and that's going to give you: one, two, three, four, five. You may also want to create a list which not only contains integers, but also mixed types. That's possible in Kotlin. You can just type list of let's say three and the character symbol A and 3.8 and a string string and it's going to evaluate to just this list. Next you may wonder how you can store these values. That's pretty simple. You can create a variable using the Var keyword which is going to be bolded and marked blue, and you can give it any name you want, basically. Let's call it string equals, and then you can put in anything you want. Let's say Kotlin sucks. No you don't do that, let's say Kotlin rocks again. Now if you press control enter it's not going to give you anything because it doesn't have a value. You just made an assignment. If you want the value of your assignment you're going to have to type in the name of your variable which was string, and you would get Kotlin rocks. Now similarly, let's create another variable which is going to hold the list. We're going to say var to create a variable call it list equals and we know how to create lists. In Kotlin we can't say list of, and one, three, five, seven, nine. All right. Now how do we get our list? We are going to type list control enter, and it's going to give us the value of our variable. Now we're going to go into arrays and list later. Now what you also might want to do is access a single element from a list. Let's say you want to access the second element or you want to have the seven of list. You can do that by typing the name of your variable first and you're going to have to use square brackets and zero is the element at first position, so it starts with zero and not with one. If you let it evaluate this statement it's going to give you one. That's the first element in our list. Similarly, you can ask it for the second element by typing a one inside the square brackets, and that's going to give you five. Now what do you think is going to be lists at position four? Well, it's not going to be the seven because we're starting with zero as the index of this is going to be zero, one, two, three, four, so it should be a nine and that's the case of course. Now we're going to talk about variables in more detail later on, but this is like a playground for you. If you want to try any statements or type in any mathematical formulas, you want to try all you can do so just remember to not only click enter but control enter to read you that statement evaluate. All right. Thanks and see you in the next lesson.