Название: Programming Kotlin Applications
Автор: Бретт Мак-Лахлин
Издательство: John Wiley & Sons Limited
Жанр: Программы
isbn: 9781119696216
isbn:
NOTE If you're wondering how Kotlin actually knows what type is allowed for a variable, keep reading. Much more on that shortly.
KOTLIN HAS A LARGE NUMBER OF TYPES
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
Kotlin gives you four options for integers, largely varying based on range. These are shown in Table 2.1.
TABLE 2.1: Kotlin Types for Integers (Nondecimal Numbers)
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
Kotlin gives you a couple of options for decimals, as well: two, again, based on precision and size. Those are listed in Table 2.2.
TABLE 2.2: Kotlin Types for Decimal Numbers
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 joker = 'j'
A character should be enclosed in a set of single quotes. You can also enclose special characters in quotes and precede the character code with a backslash. For instance, tab is \t
, a line feed is \n
, and a backslash is itself \\
:
val special = '\n'
NOTE Backslash is weird because it is itself an escape character. To get an actual backslash, you use the escape character ( \
) and then another backslash ( \
), which gets you \\
.
For sequences of characters, you likely want a String
. You can create a string with text enclosed in double quotes:
val letters = "abcde"
Simple enough! But note that a single letter in double quotes is a String
, while a single letter in single quotes is a Char
:
val letter = "a" val notStringy = 'a'
Truth or Fiction
For true or false values, you can use a Boolean
. You can assign to a Boolean
either true or false:
val truthful = true val notTrue = false
However, you cannot use 0 or 1 as you can in some languages. A 0 is an Int
(or a Long
, or even a Float
):
val notBoolean = 0 val zeroInt = 0 val zeroLong = 0L val zeroFloat = 0F
A 0 is never a Boolean
, though.
Types Aren't Interchangeable (Part 1)
Most of these types are likely unsurprising. They're typical for most languages. What will surprise you a bit is how far Kotlin will go to ensure you don't get your types wrong. Remember, Kotlin is strongly typed, and the language takes that pretty seriously.
To see this in action, let's add some basic information to the Person
class. Listing
СКАЧАТЬ