Primitive Types & Strings

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

This lecture covers all basic types available in Kotlin plus strings to store text.

Basic types in Kotlin include Byte, Short, Int, Long, Float, Double, Boolean, and Char. This lecture guides you through each of them and their differences.

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, so here we are again in the Kotlin REPL. In this lecture, we're going to talk about the basic data types Kotlin has to offer. Data types are basically the types of your variables. What we've seen so far are strings, which is a data type And strings basically represent texts. We've seen this before. You can define a string by opening up double quotes and just type in whatever you like like Kotlin, press control and enter and it's going to give you the value Which is just the text you put into the double quotes. But we've also seen our integers, and we've also had some floating point numbers. Now, in this lecture, we're going to go into more detail and look at each of the basic data types. First of all, let's recap the string again. If I want to create a value or a variable of type string I can just type val, for example, to create a value And call it whatever I like, equals I laugh I scream. Now, with strings what we can do is we can concatenate them pretty easily. We can just use the plus because plus is defined as a concatenation for strings. We can say hello Kotlin by concatenating these two strings containing hello and then a space and Kotlin. This evaluates to the concatenation of the two strings. Whenever you want to store a text, you can use a string variable or a value. Now, the next data type I want to talk about is a Boolean. A Boolean is pretty much the simplest data type Because it can have only two values which represent true or false. If I type in something like three less than four What this is going to evaluate to is true. Similarly If I type four less than three, this is going to give me false. These are the two values a Boolean variable can take on. Again, I can store a result like this in a variable or value and let's call it my Boolean And this is seven less than four. My Boolean should be false. All right, so we're going to talk more about Booleans when we talk about conditionals Because the condition will be a true or false statement. Now, next there are various ways to represent numbers And this basically only differ in the amount of numbers they can take on. The more memory the data type takes, the more numbers it can store. Now, the smallest of these data types for storing numbers is a byte. A byte takes, well, one byte of memory and it can store numbers between minus 128 and 127. So let's create a variable of the type byte. Now the problem is if I type val B equals four, by default Kotlin is going to use an integer to store this value. I have to explicitly tell Kotlin to make this a byte and I can do that by specifying the val Q at first as always then the name of the variable I wanted to find, and then I put a colon and the name of the data type I want to use like byte, and let's give it the 127. Now, you don't see any difference compared to just using int in this case but it takes less memory on your computer And I can print this as always by just typing the name of the variable and pressing control enter. The next in line is called a short. A short can take numbers between -32,768 and 32,767. That's already quite a bit more and let's just create one like this. The important thing to note is that we can specify the type of a variable or value explicitly by putting a colon behind the name of our variable and then specifying the name of the data type you want to use. Let's go on. The next one is our integer And we can do this the same way we did before by typing in int as the name of the data type. But as I mentioned, we could do the same thing by just saying equals seven because by default, Kotlin is going to use an integer to store this. Now, an integer can take on pretty large numbers. We're talking about a range from minus two billion and something to two billion and something. This is sufficient in most use cases and that's why the integer is the default data type for storing integers like this. But if we do need really large numbers, we can still use a data type that can store even more numbers And that one is called long for a good reason. And well how do we do that? We can create a variable again by specifying the name and the data type And we can put in a pretty large number here. Don't want to go too far but this should be all right. If we scroll down, we're going to see what it did and we can print the value. All right. Those are all the types that we can use for integers. I want to shortly mention another way you can specify a long variable and that is if you type something like three and eight upper case L that's also going to tell Kotlin to make it a long variable. I have to scroll down. Now, that's pretty overkill just to store a three but you get the point. Next we also have to store floating point numbers Which we've also already done before. But there are actually two different data types for storing floating point numbers. The first one that takes less memory and is less precise is called a float. So let's just call this one F and this is a float Which we're going to give the value 3.73. If you scroll down, this is actually going to give us an error and that's Because I forgot to tell Kotlin explicitly that this is a float because by default Kotlin uses double values to store floating point numbers like these. For Kotlin, this is a double and does not implicitly transform this double value into a float. To do that, we're going to have to tell Kotlin to explicitly make this a float. We can do this similar to the long variable. We can put a lowercase F at the end And this is going to tell Kotlin to make this a float variable. Now there is no error anymore and we can print out our value. Now, since double is the default value just like integer We can actually create a double value by just typing something like 3.02 And there's going to be a double value. But of course we can also do this explicitly again by specifying the type we want to use And again, assigning some value. If we scroll down, we see what it has done and we can print the value again. All right, there's one more to go And that is storing a single character. Pretty much every programming language has a separate data type to store single characters And that's called char or a character, in some programming languages. To create value of type char, we can just do the same thing again and this is char Which is just a short notation for character. But the data type is called char in Kotlin. When we define a char, we have to use single quotes like this. Again I have to have to scroll down first and this is going to -- Well This is kind of confusing, but I can create another char let's call it c2 of type character and store lets say a 'c' inside. This is going to store this character. Now, the last thing I want to mention is how to basically switch between the types. All the number types implement functions that let you transform between them. Now, since I've only been using values before, let me just quickly create a float. Let's call it f2, because I've already used F and assign 3f. This time I'm making it a variable so that I can reassign it. And d3 is a double of 0.0. Let's try to assign the value of f2 to our double. If you try to just say D3 is supposed to be f2 This is going to give us an error Because Kotlin again does not want to implicitly transform the flow to a double value. But what we can do is we can explicitly call a function on f2 called todouble. This is going to allow us to do this assignment, and now if you print d3 This is going to give us the value that was stored in the float before. But note that since the float is not as precise as the double value This is actually not exactly 3.4. This is also why floats are not commonly used Because they are very dangerous due to their imprecise storing of the data. Now, there are similar functions for all the other types like toInt, or toLong, or toShort And you can play around with them in the REPL And try them out for yourself to get used to transforming between the data types. All right, so now we know all the basic data types we need to know in Kotlin And I will see you in the next lecture.