Участник:Alexsmail/Программирование 2020/новый черновик: различия между версиями

Содержимое удалено Содержимое добавлено
м викификация
черновик
Строка 499:
 
=== * Advanced. Системы счисления. Непозиционные системы счисления. Позиционные системы счисления. Двоичная система счисления. mod 2^32. Дополнение к 2. ===
Boolean: boolean
 
Numeric primitives: byte, short, int, long, float and double. These primitive data types hold only numeric data. Operations associated with such data types are those of simple arithmetic (addition, subtraction, etc.) or of comparisons (is greater than, is equal to, etc.)
 
Textual primitives: char, String. These primitive data types hold characters (that can be Unicode alphabets or even numbers). Operations associated with such types are those of textual manipulation (comparing two words, joining characters to make words, etc.). However, byte and char can also support arithmetic operations.
 
byte 8 -128..127
char 16 0..2^16-1 Note that characters are not numbers in Kotlin.
short 16 -2^15..2^15-1
int 32 -2^31..2^31-1
long 64 -2^63..2^63-1
float 32
double 64
 
For efficiency's sake, this behavior is implementation specific; Java virtual machines are free to perform writes to long and double values atomically or in two parts.
Some processors do not provide the ability to write to a single byte.
===
Characters
 
Characters are represented by the type Char. They can not be treated directly as numbers
 
fun check(c: Char) {
if (c == 1) { // ERROR: incompatible types
// ...
}
}
 
Character literals go in single quotes: '1'. Special characters can be escaped using a backslash. The following escape sequences are supported: \t, \b, \n, \r, \', \", \\ and \$. To encode any other character, use the Unicode escape sequence syntax: '\uFF00'.
 
We can explicitly convert a character to an Int number:
 
fun decimalDigitValue(c: Char): Int {
if (c !in '0'..'9')
throw IllegalArgumentException("Out of range")
return c.toInt() - '0'.toInt() // Explicit conversions to numbers
}
 
Like numbers, characters are boxed when a nullable reference is needed. Identity is not preserved by the boxing operation.
=====
However, to support generic use cases and provide total ordering, when the operands are not statically typed as floating point numbers (e.g. Any, Comparable<...>, a type parameter), the operations use the equals and compareTo implementations for Float and Double, which disagree with the standard, so that:
 
NaN is considered equal to itself
NaN is considered greater than any other element including POSITIVE_INFINITY
-0.0 is considered less than 0.0
 
 
 
 
https://en.wikibooks.org/wiki/Java_Programming/Primitive_Types
style="border: 0px; background-color: white;"|
 
{| class="wikitable" style="margin: auto; border: 0px;"
| colspan="8" style="border: 0px; background-color: white; text-align: center;"| ''Memory state''
| style="border: 0px; background-color: white;"|
| style="border: 0px; background-color: white; text-align: center;"| ''Gives''
|-
| &nbsp;<code>0</code>&nbsp;
| &nbsp;<code>0</code>&nbsp;
| &nbsp;<code>0</code>&nbsp;
| &nbsp;<code>0</code>&nbsp;
| &nbsp;<code>0</code>&nbsp;
| &nbsp;<code>0</code>&nbsp;
| &nbsp;<code>0</code>&nbsp;
| &nbsp;<code>0</code>&nbsp;
| style="border: 0px; background-color: white; text-align: center;"| &rarr;
| style="border: 0px; background-color: white; text-align: center;"| 0
|-
| &nbsp;<code>0</code>&nbsp;
| &nbsp;<code>0</code>&nbsp;
| &nbsp;<code>0</code>&nbsp;
| &nbsp;<code>0</code>&nbsp;
| &nbsp;<code>0</code>&nbsp;
| &nbsp;<code>0</code>&nbsp;
| &nbsp;<code>0</code>&nbsp;
| &nbsp;<code>1</code>&nbsp;
| style="border: 0px; background-color: white; text-align: center;"| &rarr;
| style="border: 0px; background-color: white; text-align: center;"| 1
|-
| &nbsp;<code>0</code>&nbsp;
| &nbsp;<code>0</code>&nbsp;
| &nbsp;<code>0</code>&nbsp;
| &nbsp;<code>0</code>&nbsp;
| &nbsp;<code>0</code>&nbsp;
| &nbsp;<code>0</code>&nbsp;
| &nbsp;<code>1</code>&nbsp;
| &nbsp;<code>0</code>&nbsp;
| style="border: 0px; background-color: white; text-align: center;"| &rarr;
| style="border: 0px; background-color: white; text-align: center;"| 2
|-
| &nbsp;<code>0</code>&nbsp;
| &nbsp;<code>0</code>&nbsp;
| &nbsp;<code>0</code>&nbsp;
| &nbsp;<code>0</code>&nbsp;
| &nbsp;<code>0</code>&nbsp;
| &nbsp;<code>0</code>&nbsp;
| &nbsp;<code>1</code>&nbsp;
| &nbsp;<code>1</code>&nbsp;
| style="border: 0px; background-color: white; text-align: center;"| &rarr;
| style="border: 0px; background-color: white; text-align: center;"| 3
|-
| colspan="8" style="text-align: center;" | <br/>...<br/>
| style="border: 0px; background-color: white; text-align: center;"| ...
|-
| &nbsp;<code>1</code>&nbsp;
| &nbsp;<code>1</code>&nbsp;
| &nbsp;<code>1</code>&nbsp;
| &nbsp;<code>1</code>&nbsp;
| &nbsp;<code>1</code>&nbsp;
| &nbsp;<code>1</code>&nbsp;
| &nbsp;<code>1</code>&nbsp;
| &nbsp;<code>1</code>&nbsp;
| style="border: 0px; background-color: white; text-align: center;"| &rarr;
| style="border: 0px; background-color: white; text-align: center;"| 255
|}