Transcript from the "Variables & Types" Lesson
>> Variables & Types. So I'm now I'm opening this Lesson 2 that we have here, okay? Remember, kts is just this simple way that we have to see some Kotlin codes. We can execute this code, but without attaching this to an actual app, okay? That's kind of the idea, this is not an app, we are not building an app.
[00:00:20] We're just playing with Kotlin and for that we use this kts file format that we'll never see again, we will never see this format again later, okay. All the files will be just kt, that's the extension for Kotlin. So we mentioned that it's an statically typed language. So actually every variable will have a type.
[00:00:41] So in this case, data will have a type even if I don't see explicitly any type. If you hover with your cursor, you will see there that it says, integer is really small, okay? But let me see if I can zoom in a little bit, like so. So you can see it says, the data is an int.
[00:01:03] So it's actually using, It's actually inferring the type based on the value that I'm setting. But if I want to, I can use colon and the type, such as Int, capital I, because it's a data type, okay? So be careful if you're coming from other languages where int is with lowercase i.
[00:01:31] So this is a mutable variable, we use var for variable, okay? But we also have immutable variables with the keyword val from value, val, not var. By the way, do you know the difference between an immutable variable and a constant? Does anyone know the difference between?
>> It's compiling time.
>> Compiling time, it has to do with that, exactly. A constant value is something that needs to be defined at compilation time, when you're compiling the app, the value must be set, that's a constant. An immutable variable, it's a variable, so it's actually a place in memory when you're executing the app, but the idea is that once you set the value, it's immutable, so you cannot change that.
[00:02:16] So for example, in Kotlin we have both, constant and immutable variables. Typically we use immutable variables. Immutable variables can be set with values coming from a database, from the network, from what the user is typing. A constant, no, a constant must be set at compilation time. So a constant is actually const val, that's how you set that.
[00:03:02] But here it's val. Then types, as I mentioned before, we use colon, okay, but if not, they are inferred. Similar to other languages with type inference, we might have a problem. For example, we have a price that is a double, other price, it's also double, because I'm setting a value, even if I'm not explicitly saying double.
[00:03:23] But sometimes we want the double, but we are getting an integer. That's because 23 is an integer. Well, we can solve the problem with .0, in that case it's a double. So, .0 will force a double. Also you can force explicitly the type. In case you have this ambiguity, you can resolve those ambiguities by defining the type.
[00:03:53] A double is by default a double precision float. We also have the type float, and there is an F that we can add here to explicitly say that we don't want a double, we want a lower precision float, okay? We don't going to use this a lot actually, okay?
[00:04:12] But have in mind it's there. We have a structural equality, that's double equals, and triple equals is referential equality. So the first one is actually like executing the equals method of that object to see if the contents are equal. For example, dual strings will give me true, but will give me false on referential equality because I'm actually making two objects, okay?
[00:04:58] Core data types, strings, characters is char, int, double, Boolean, and we have Any. We don't typically use Any a lot, but Any means, well, in this variable I can hold any kind of option. And then you need to cast or ask for a type before actually using it, okay?
[00:05:17] As I mentioned, we have float, also for integers, we have bytes, shorts, and longs. So integers actually goes between short and long in terms of how many bits it's using. We use double quotes for strings, okay? We also have a way to create a multiline string using triple double quotes, okay?
[00:05:42] And this is multiline, and the multiline, it's actually pretty good, for example, for JSON because now you don't need to escape, The quotes, okay? So, something like that. On every string we have template expressions. Template expressions are using the dollar sign when it's just the name of a variable.