Quick Differences Between Kotlin and Java

Tim Buchalka
A free video tutorial from Tim Buchalka
Java Python Android and C# Expert Developer - 912K+ students
4.6 instructor rating • 12 courses • 924,545 students

Learn more from the full course

Kotlin for Java Developers

Use your Java skills to learn Kotlin fast. Enhance career prospects and master Kotlin, including Java interoperability

16:41:26 of on-demand video • Updated January 2021

  • Create Kotlin programs
  • Understand the differences between Java and Kotlin
  • Understand how Java and Kotlin programs interoperate
English In this video I'm going to cover the lightning quick differences, differences that are just informational or they don't require a lot of explanation. So, the first one I'm going to start with, which I'm sure you've already gathered is that you don't have to put semicolons at the end of a statement. Now, it's okay if you do it, let's put a semicolon here at the end of this line and we'll see that you don't get an error, but IntelliJ grays it out and it says redundant semicolon, whenever you see something great out in IntelliJ it usually means that you're not using it, like for example, employees is grayed out here because we're not using it or you don't need it, which is the case for the semicolon. Now this might be a hard habit for you to break, I didn't find it that hard to break actually. Now when I go back to Java though, I have to remember to put the semicolon, so there is that. But if you're finding it a hard habit to break, at least it's not an error but obviously the more you work with Kotlin, you don't want to be putting semicolons there because with Kotlin the more concise the code, the better. So that's difference number one. So the next thing I want to bring to your attention isn't really a difference, but they're used a lot in Kotlin so I want you to know about them and that's the use of wrappers. We saw one already in the hello world application when we use print line rather than system.out.println, now under the covers we were still using system.out.println, but the Kotlin standard library has wrappers for some of the more commonly used Java methods, so I just wanted you to be aware of them because you'll often see the term wrapper when people are talking or writing about Kotlin. Okay next is the concept of soft keywords, now in Java you can't use a keyword for anything other than its intended purpose. So if a word is reserved in Java, it's a keyword you can only use it for what it's intended to be used for. You can't use it anywhere else. Kotlin has the notion of hard keywords and soft keywords. Now hard keywords are just like Java, you can only use them for their intended purpose, but soft keywords are a little bit more relaxed. You can actually use them in other places as long as you're not using them where they have a special meaning. So for example you could use a soft key word as a variable name. If you want it to, as long as you know you're not using that variable right where you would normally use a soft key word as its intended to be used. Now personally, I wouldn't do this, I think that it could lead to some pretty confusing code and it might run you into trouble at some point, but you can do it the language won't stop you from doing it and once again just like wrappers, I want it to bring them to your attention, because you might see the term soft key word and wonder what people are talking about. Alright, another difference between Kotlin and Java and this once again is in the pursuit of more concise code, is that you can use square brackets to access elements in collections. Now, we'll cover collections later in the course, but I'll create one now just to illustrate this. So let me make a few blank lines here and I'll say val names and I'll make this an array list, we don't need the angle brackets because the compiler will figure it out and we'll say John, Jane and Mary. Now if we want it to print out Jane or the second element, we can just go print line names one and that will work. So if we run this, we'll see that Jane is printed out so this is a way that Kotlin lets us write more concise code, instead of using names.get, we can just use the square brackets as if the collection is an array and note that when you're working with a map, you can also use the key as an index and we'll see that later when we cover collections. Okay, another informational sort of called out and that's the string class. Now, we use strings a lot in our java applications, Kotlin has its own string class and I wanted to draw your attention to that, because its class hides some methods that exist in the Java string class, so when you're using string in a Kotlin application you're using Kotlin string class and so if you're trying to call a method that you would normally call in the string class and it's not compiling or it doesn't exist, this will be why. Let's pop over to the documentation for the Kotlin string class, so here we are at the documentation and you can see that it's in the Kotlin package, where a lot of the data type classes live, in fact I think all of them do. And as we can see, it's a pretty big class, so I suggest that you come over and take a look at this class. One thing I will point out is the length property in Java of course length is a method but in Kotlin length is a property, so if you want to get the length of a string there's no reason to call a function, you'll just access the property. So as I said, take a look at the string class, because when you're writing a Kotlin application and you're using string, you're actually using this class you're not using java.line.string. Oo it's possible that something might have changed about a method that you use a lot. So I suggest that you pop over and take some time to look at the string documentation because of course string is the very popular class, we use it all the time. Let's go back to IntelliJ now. Okay so another important difference between Kotlin and Java is exceptions, in Java you have to declare when a method can throw a checked exception, for example if a method can throw an io exception and it doesn't catch it in its code then it has to declare that it throws io exception. Well, not so in Kotlin because Kotlin doesn't distinguish between checked and unchecked exceptions, so you no longer have to declare that a function can throw an exception, in fact the throws keyword doesn't exist in Kotlin so if you try to declare which exceptions the function throws, it's not going to compile. So if you're wondering why you never see functions throwing any exceptions, that's why. It's not that they can't raise exceptions, it's just that all exceptions are unchecked and so you don't have to declare them. Okay next the ternary operator and if you forgotten what ternary operator is, it's when you do something like X,Y,Z, so you know this is, X is the condition and then if it's true, normally there's a variable here you know what my q equals X, Y, Z. So, if X is true Y gets assigned to Q and if it's false that gets assigned to Q well this doesn't exist in Kotlin, it's been replaced by the if statement, in fact in Kotlin if is an expression. So we will be covering this in more detail later, but for now just know that the ternary operator is gone. No longer there in Kotlin. Okay, the next difference involves the for loop. Now we're going to cover loops in more detail later on, for now you just have to know that the original for loop that we all know and love doesn't exist in Kotlin. So in Kotlin you cannot do this, for i, well let's go into well that wouldn't even work in Kotlin, but i equals 0, i less than you know 20. i++ and then you do something. This form of the for loop doesn't exist in Kotlin. Of course in Java, you probably have this. But this doesn't exist, Kotlin doesn't understand this and as I said, we'll go over for loops in much more detail, later in the course. So another difference between Kotlin and Java is that Kotlin doesn't have a static keyword and we'll see as we progress through the course, how Kotlin has replaced this keyword because it depends on whether you're dealing with a function or whether you're dealing with a variable. Now one way that it has done it of course is with top-level functions and we've already seen that. Here, we have the main function outside of a class and under the covers, this is actually a static function. Now when I say that the static keyword is gone, the concept of static is still there, because don't forget Kotlin when you're targeting the jvm is being compiled down to java bytecode which understands static and what static. So, when we say it's not there we mean syntactically, the concept is still there it's just that the way that you deal with static functions and static variables is different and we'll see the different ways as we progress through the course. Okay, the next difference which I'm sure you've also already gathered is that there is no new keyword, so we never have to use new and this is just another way of Kotlin making it more concise, I mean if the common pattern when you're creating an instance is to always go new and then you know the class name with an uppercase letter and then the constructor parameters. Why do we need new? The compiler can figure out what you're trying to do just with this part of the statement here, so Kotlin has dropped new and you don't use it in Kotlin. Okay, so that's it for the lightning quick differences. Now, obviously there are a lot more differences between Kotlin and Java. I mean, you know in in some sense that's what the rest of this course is about, but if you're trying to get your feet wet by writing some Kotlin code, now you already know a few things that you can't do. And that's it for this video.