Being 100% Java compatible, the type system in Kotlin is identical to Java, except that its defaults are different. For example, while Java defaults to float for floating point data, Kotlin defaults to double. Also learn about type inference in Kotlin, wh
- [Instructor] One of the first features everyone takes note of in Kotlin is it's type inference system. Type inference means that unlike Java, where every variable declaration begins with it's type, in Kotlin, a variable begins with either the val or var keyword, followed by the variable's name and definition. The only time we need to specify the variable's type is when the compiler is unable to determine it. Take, for instance, the following declaration. We create a val, give it a name, aSentence, and give that sentence a string.
Here, it is pretty obvious that aSentence is a string. There isn't anything magical about the type inference. It is simply the compiler taking over a redundant task from the programmer. If the type can be inferred, it doesn't have to be specified. Here are a couple more examples. Create another val, this time we'll call it myPi. Set it equal to 3.14, and we'll say val, myAnswer, equals 42.
If we don't give the variable a definition, then we must specify the type. Take the following examples. Here we'll set var, aString, to String. Now because we don't give it a definition, we must give it a type. Likewise, when we say val, aDouble, set that to Double, and val, aInt, we have to set that to Int for it to be an integer.
In these examples, there is not enough information for the compiler to determine what the type of each variable is, so the programmer must specify it or there will be a compile time error. Types in Kotlin are nearly identical to those in Java. Remember, interoperability is a key design goal of Kotlin, so we have the following built-in types. So we have var, myDouble, and we'll set that equal to 1.999.
Likewise, we can create a float, we'll say myFloat, and we'll set that equal to 1.9. Now, if we want this to be float, we have to put either a small or upper case F on the number, in order to specify that it's a float. Likewise, when we say var, myLong, we want this to be a long, we've got to appended with an L, either lower case or upper case.
We have myInt again. Then we have a short value. Now for sure, there is no abbreviation, we just say Short equals, say, 12, and we have a byte, which is eight bits, and we have to specify byte after it, and we can say, like, 127.
Now, you notice, if we go on byte, if we go one more, it will give us an error, basically telling us that hey, we've gone too far, because a byte only goes from 128 negative to 127 positive. Kotlin doesn't automatically widen numbers during conversions, and doubles are the default when a number has a decimal point. If you, like, go with float, you must append an F to the number. To make numeric literals more legible, you can use an underscore to separate digits.
The underscore doesn't affect the value of the literal, it just makes it easier for us humans to read. So we say val, aLongNumber equals 123 underscore 456 underscore 789. This number here is equal to this number. The only difference between the two is that the first one is easier to read than the second one.
You must explicitly convert numbers, even in the case of going to a wider number, as in this example, so here we're going to take an integer, and give it a value, and we're going to create aLong, and we're going to try to initialize the long from that integer, and if we try, what ends up happening is the compiler says we can't do it, that you can't just put an Int into a Long, even though it's a wider value.
In order to do so, Kotlin requires you to be explicit about conversions, and provides a set of converters, so in this case, what we need to do is, say, toLong, and now it will take it. Now you have a pretty good idea about types in Kotlin and how type inference works.
- Kotlin as a better Java
- Setting up a Kotlin programming environment
- Val vs. var
- Understanding basic Kotlin programming concepts
- Object-oriented programming
- Using Java from Kotlin
- Using Kotlin from Java
- Annotations, reflection, and DSL construction
- Functional programming in Kotlin