Using the Double data type, and determining class types in Kotlin

Matt Greencroft
A free video tutorial from Matt Greencroft
Course tutor at Virtual Pair Programmers
4.6 instructor rating • 10 courses • 49,771 students

Learn more from the full course

Build Spring Boot Apps with the Kotlin Programming Language

Build fully functional, robust and efficient applications with Spring Boot and the Kotlin programming language

07:56:16 of on-demand video • Updated October 2019

  • Build fully functional, robust and efficient applications with Spring Boot and the Kotlin programming language
English [Auto] Hello again and welcome to chapter three in the last chapter we looked at strings in this chapter I want us to look at the other standard data types and we get Jarba the integer float double and so on. Well a good starting point to say is that all of these object types do exist in cotland and you can use them just as you would expect to. We can declare it doubled for example like this. Let's say Val would light up on depos And let's give it the number of 21 point. Just as we saw in the last chapter with strings the data type can be inferred. Now in Scotland there were no literal types so this object will be a type double with a capital D rather than double with a lowercase day. And we can check that because in Scotland there is a keyword is which is the equivalent to jobber's instance of. So I can do something like print like is my double double questionmark and let's print them out using the dollars kind of brackets expression we can say might double is double. So we should get a true if my double is an object of type double or false. Otherwise let's just run that and check that we get a true yes it is look weak as a true up there. So the key word is useful to check and objects data type. But what if we want to know what the objects data type is what if we want a printout something or like my data on these and any dollars curly brackets might double dot and in Java at this point we'd call the in method class. Now there is no class method here. As you can see there is a Java class and I'm going to come back to that in just a moment. But in order to find out the class type we need to use a slightly unusual looking syntax and that's because it uses something called reflection. Now reflection isn't a bad topic I'm going to cover it right at the end of the course. So for now when ever you want to find out the class type of a variable just remember that this is what we need to type in it's the variable name. Then two columns then the word class and then dot and finally qualify. Net. So this is the way Koplin we find out the class type of a variable. Let's run that OK and we can see that my double is a couple in double. So what is 8 Coplan double. Well it's really a Java double but with a few extra bits of functionality. If I type in my double followed by a ADOT we will see that compared to Java. There are some other methods on Heia which don't exist in Jarba for example there is a two big decimal method which will convert our double into big decimal. I think that's quite nice and there are some rounding methods down here I think as well. There is round and round a lot. So those again are quite nice methods and they work exactly like you expect. We'll have a look at one of these in just a moment. So Colin double has some extra methods other than a job double but there's also a little change in that as if you meant well I said there were no little was in cut them but that's how we look at that jump class a method that we saw just a moment ago. Let's print out this time my doubles the Java class is dollars. My Double Dot Java class. And let's see what result we get there. Well it's Java class is doubled but with a lowercase day. So a Cartland double will be compiled to a java and will double. So although there are no literal was in Cartland So every double or integer is always an object actually it the compound and too literal to be run on the JVM.