From the course: Build Spring Boot Apps with the Kotlin Programming Language

Creating a new Kotlin project

- [Instructor] Hello and welcome back. We're now at chapter two, and we're finally ready to start learning some Kotlin. In this chapter, we're going to be looking at strings. Now actually, this will allow us to explore how to declare variables more generally. Although, we're going to look at some of the other object types in the next chapter. Before we start, I want to set up a new Kotlin project and with the equivalent of the public static void main that we are looking at here in Java. I suggest that you follow along with me, so that you can try out this code for yourself. We're going to click on file and new project. And this time, we want to tick Kotlin JVM. Now, I should say something about this. When we write JAVA code, we use the JAVA Runtime libraries, and they come with the JVM. Kotlin uses its own set of class libraries, the Kotlin Runtime, so when we tick this box, what we actually get in our project is access to all of the JAVA classes and all of the Kotlin classes. When we build a project, we need to include the Kotlin Runtime JARs in that project as these will be needed, so that it can run on the JVM. Now, IntelliJ takes care of this, so we don't need to worry too much about it, but I wanted to just explain, on this screen what we're really doing is creating a JAVA project with the additional Kotlin JVM libraries and frameworks added in. So, let's click on next, and we'll call this project learning Kotlin I think. I don't need this hello world application, so I'll replace this window with our new project. Before we actually start writing any Kotlin code, this is a JAVA project, and we can create JAVA classes in here if we wish too. In fact, I'm going to do that, so that I can write some JAVA code and compare it to Kotlin. The first thing I think I'm going to do is just create a package for my JAVA code to sit in. Now, I'm going to suggest that you don't need to type in this JAVA code. You can just concentrate on the Kotlin code, so you might just want to create a package for your Kotlin code, but I'm going to create both a package for Kotlin and a package for JAVA. You don't have to have JAVA code in a separate package if you're using JAVA and Kotlin in the same project. I'm just doing this to keep things clear for the purposes of understanding exactly what we're doing. So under SRC, let's create a new package, and I'll call this one com.virtualpairprogrammers.learningkotlin.java, and hopefully we can see it there. And, I'm going to create a second package for my Kotlin code, so that will be com.virtualpairprogrammers.learningkotlin.kotlin. Okay so later on, we'll be building some more sensible looking software, and we'll have some more sensible looking package names, but that's enough to get started. Now, I'm not going to bother creating the hello world in JAVA because we know what that looks like, so let's go and create our first Kotlin class. In the Kotlin package, we'll right-click and choose new, and this time we're going to pick Kotlin file/class. Now, you'll notice there is a dropdown here from which we can choose whether you want a file, a class, an interface, and so on. For now, let's just work with a file. We will be talking about classes a bit later on, but file will do for now, and we'll give our file a name, and I think we'll call this one exploring variables. Just like in JAVA, we should name our classes or files starting with a capital letter. So, we'll click on OK, and what we now have is an empty file just with our package declaration at the top. Now in this file, we can create classes, for example, but for now I just want to create the equivalent of a public static void main method. In IntelliJ, there is a really quick way to do this. You can just type in the word main and press the enter key. Now, the code we're looking at here is the equivalent to a JAVA public static void main method. There are quite a few differences. The syntax is very different. That's quite obvious, and we will be talking about the syntax of methods quite soon. But, the other difference is that this function is living outside of a class, and that's fine in Kotlin. Functions can exist as top-level objects. Again, we'll be going into a lot more detail about this as we progress through the course. But for now, let's just write some code in here to print out the phrase hello world, and we can run this code to check it's working. Now, although this isn't the common way of doing things, I'm going to use the JAVA code that we're all familiar with for now. So, inside this function, we'll type in system.out.println, big quotes, hello world, and a semicolon on the end. We can run this code by clicking on the green triangle here. So, we'll click on run, and hopefully in a moment or two, we'll get a terminal window pop up with the phrase hello world printed out. There it is. Just to point out, if you've not used IntelliJ before, I did actually say this in the last chapter, you can rerun the last code that you ran at any point, by clicking on the green triangle up here.

Contents