Programming Kotlin Applications. Бретт Мак-Лахлин

Читать онлайн.
Название Programming Kotlin Applications
Автор произведения Бретт Мак-Лахлин
Жанр Программы
Серия
Издательство Программы
Год выпуска 0
isbn 9781119696216



Скачать книгу

The point is important, though: naming matters.

      Normally, you'd need to make a change here, but again, the IDE has done some nice work for you. Check out your file with main in it ( PersonApp if you've followed along); it also has a new line at the top:

      import org.wiley.kotlin.person.Person

      This line tells Kotlin to import, or make available, the org.kotlin.wiley.person.Person class without needing to refer to it by its complete name. It essentially says “import that long name and allow it to be referenced by just the class name: Person.”

      NOTE You could also leave out that top line of the main file and just refer to the class by its entire name every time you use it. So you'd create a new instance like this:

      val brian = org.wiley.kotlin.person.Person("Brian", "Truesby")

       You'd have to do this for every reference to Person , though. Developers don't typically like this, as it's a lot of extra typing, so using import is a lot more common.

      With all these changes in place, you can recompile and run your program again. Things should work beautifully (although that last name is still wrong!).

      Classes: The Ultimate Type in Kotlin

      Before getting further into Kotlin types—something this chapter is going to spend the rest of its time on—it's worth saying that classes are really the ultimate type in Kotlin. A class provides you a way to collect data and work with that data in a specific manner, and to model things in the world: numbers, sentences, objects like cars, people, even abstract ideas like a radio wave or a decision.

      Classes also give you a pointer into something that's quite important in Kotlin: type safety. Type safety refers to the degree to which a programming language keeps you from making mistakes related to assigning one type (like a number) to a variable that should only hold letters. There's nothing as frustrating as treating a variable like it's all letters, and things in your program break because it turns out that that variable actually contains an instance of Person (or Car, or User, or something else that doesn't at all act like letters).

      And that's where classes and objects are so key to type safety. Your Person is now strongly typed; you can't create an integer and shove a string into it. More specifically, you can't create a Car and shove it into a Person. You're going to see a lot more about this as the chapter goes on. For now, though, just think of objects as a really powerful way to be sure a variable (whether by val or by var) has exactly in it what you need.

      NOTE If you're wondering how Kotlin actually knows what type is allowed for a variable, keep reading. Much more on that shortly.

      Like any programming language, Kotlin supports lots of basic data types. You can define integers ( Int), sequences of letters ( String), decimal numbers ( Float), and a lot more. Let's take a blazing-fast run through the basic types and then start putting them to use.

      Numbers in Kotlin

TYPE SIZE (BITS) MINIMUM VALUE MAXIMUM VALUE
Byte 8 –128 127
Short 16 –32,678 32,767
Int 32 –2,147,483,648 (–231) 2,147,483,647 (231 – 1)
Long 64 –9,223,372,036,854,775,808 (–263) 9,223,372,036,854,775,807 (263 – 1)

      Kotlin will largely take care of figuring out which type to use when you don't declare that type explicitly. In that case, it's going to look at the value you're assigning the variable, and make some assumptions. This is a pretty important concept called type inference, and it's something we're going to talk about in a lot more detail in Chapter 6.

      If you create a variable and assign it a number that fits into Int, then Int will be used. If the number is outside the range of Int, the variable will be created as a Long :

      val someInt = 20 val tooBig = 4532145678

      So here, someInt will be an Int. tooBig is too big to fit into an Int so it will be a Long. You can also force a variable to be a Long by adding a capital L to the value:

      val makeItLong = 42L

TYPE SIZE (BITS) SIGNIFICANT BITS EXPONENT BITS DECIMAL DIGITS
Float 32 24 8 6–7
Double 64 53 11 15–16

      Assignment here works a bit unexpectedly. Decimal variables will be Double unless you tell Kotlin to use Float by using the f or F suffix:

      val g = 9.8 val theyAllFloat = 9.8F

      Letters and Things

      If you want to represent a single character—including special characters like backslash or a carriage return—you use the Char type:

      val