Boolean type

Sample code:

val aBoolean: Boolean = true
val anotherBoolean: Boolean = false
Copy the code

Boolean in Kotlin is, for the most part, equivalent to the basic Type Boolean in Java, which is boxed into the Java booleans only when necessary. It’s up to the compiler to decide, and usually we don’t care.

The Number type

Number refers to a Number, including integer and floating point types:

classification type A wide
floating-point Double 64
Float 32
The integer Long 64
Int 32
Short 16
byte Byte 8

The Int type

Kotlin Int can be an integer, hexadecimal, or binary, like Java Int:

val anInt: Int = 8 / / 8
val anotherInt: Int = 0xFF / / 255
val moreInt: Int = 0b00000011 / / 3
val maxInt: Int = Int.MAX_VALUE / / 2147483647
val minInt: Int = Int.MIN_VALUE / / - 2147483648
Copy the code

An integer is a signed number. The highest bit of an integer is the sign bit, and the remaining 31 bits represent the range of values. The maximum is math.pow (2.0, 31.0) -1 and the minimum is -math.pow (2.0, 31.0)

Long

Long is similar to Int, except that Int is 32 bits and Long is 64 bits, which can represent larger numbers:

val aLong: Long = 12345678987654321 / / 12345678987654321
val anotherLong: Long = 123 / / 123
val maxLong: Long = Long.MAX_VALUE / / 9223372036854775807
val minLong: Long = Long.MIN_VALUE / / - 9223372036854775808
Copy the code

Because anotherLong enforces Long, 123 is Long, and 123 in println(123) is Int. If Long is required, I could write println of 123L like this.

A Float

A Float is a single-precision floating-point number with a decimal. A Float must be preceded by an F or F, otherwise it will be considered a Double.

val aFloat: Float = 2.0 F / / 2.0
val anotherFloat: Float = 1E3f / / 1000.0
val maxFloat: Float = Float.MAX_VALUE / / 3.4028235 e38
val minFloat: Float = Float.MIN_VALUE / / 1.4 e-45
val minFloatReal: Float = -Float.MAX_VALUE / / 3.4028235 e38
val positiveInfinityFloat: Float = Float.POSITIVE_INFINITY // Infinity, output Infinity
val negativeInfinityFloat: Float = Float.NEGATIVE_INFINITY // negative Infinity, output -infinity
Copy the code

Note that float. MIN_VALUE refers to the smallest non-zero positive number! Minimum non-zero positive! Minimum non-zero positive! To represent the smallest signed floating-point number, use -float.max_value.

Here is a partial source comment for Float in Kotlin:

/** * Represents a single-precision 32-bit IEEE 754 floating point number. * On the JVM, non-nullable values of this type are represented as values of the primitive type `float`. */
public class Float private constructor() : Number(), Comparable<Float> {
    companion object {
        /** * A constant holding the smallest *positive* nonzero value of Float. */
        public val MIN_VALUE: Float

        /** * A constant holding the largest positive finite value of Float. */
        public val MAX_VALUE: Float}}Copy the code

NaN

Float has a NaN constant property for a number that is “not a number.” When do you get NaN?

println(0.0 f / 0.0 f) // NaN
println(0.0 f / 0.0 f= =Float.NaN) // false
Copy the code

Two conclusions can be drawn from the above Demo:

  1. Divide by zero and you get NaN
  2. NaN is not equal to any NaN

NaN means “not a number,” so the comparison is meaningless.

Type Double

A Double is a double-precision floating-point number. A Double is to Float what a Long is to Int, so a Double is simply a larger floating-point number than Float.

val aDouble: Double = 3.0 / / 3.0
val anotherDouble: Double = 3.1415926 / / 3.1415926
val maxDouble: Double = Double.MAX_VALUE / / 1.7976931348623157 e308
val minDouble: Double = Double.MIN_VALUE / / 4.9 e-324
val minDoubleReal: Double = -Double.MAX_VALUE / / 1.7976931348623157 e308
Copy the code

Like Float, Double.MIN_VALUE represents the smallest non-zero positive number.

Short type

Short is also called a Short integer with 16 bits less than half the width of an Int:

val aShort: Short = 127 / / 127
val maxShort: Short = Short.MAX_VALUE / / 32767
val minShort: Short = Short.MIN_VALUE / / - 32768
Copy the code

Because Short is a 16-bit signed number, the maximum number Short can represent is 2 minus 1 15 times, math.pow (2.0, 15.0) -1.

Byte type

The Byte type is shorter than the Short type and is generally used only for binary data streams:

val maxByte: Byte = Byte.MAX_VALUE / / 127
val minByte: Byte = Byte.MIN_VALUE / / - 128
Copy the code

Basic type conversion

Java supports using a long or float variable to receive an int variable without an error. This is because Java supports implicit conversions, but this won’t work in Kotlin. Kotlin doesn’t support implicit conversions, for example:

val anInt: Int = 5
val aLong: Long = anInt // Error: Type mismatch
val aLong: Long = anInt.toLong() / / normal
Copy the code

The type Char

  • Char is called a Character, corresponding to Java Character
  • Char is a two-byte Unicode character of 16 bits
  • Char requires single quotation marks' 'Cause, for example: ‘0’, ‘a’, ‘\n’
val aChar: Char = '0' / / 0
val bChar: Char = 'darling' / / Lin
val cChar: Char = '\t' // Invisible tabs
val dChar: Char = '\u9510' / / sharp
Copy the code

Char represents a character whose value can be written as letters, numbers, Unicode, or special characters like \n.

Other symbols that are syntactically special need to be escaped with \ if you want to print them as they should be:

Escape character meaning
\t tabs
\b The cursor moves back one character
\n enter
\r The cursor returns to the beginning of the line
\ ‘ Single quotes
\” Double quotation marks
\ \ The backslash
\ $ Dollar sign, Kotlin supports a string template that begins with a dollar sign
val eChar: Char = '\' ' / / '
val fChar: Char = '"' // "
val gChar: Char = '\ "' // "
val hChar: Char = '\ $' / / $
val iChar: Char = '\ \' / / /
Copy the code

Type String

The String is a String of characters. Char is raised with “” and String is raised with “” :

val aString: String = "HelloWorld" // HelloWorld
val anotherString: String = String(charArrayOf('H'.'e'.'l'.'l'.'o'.'W'.'o'.'r'.'l'.'d')) // HelloWorld
println(aString == anotherString) // true
println(aString === anotherString) // false
println(aString.length) / / 10
Copy the code

== equals(), check whether the values are the same; === is the congruent operator, which checks whether the values are equal and whether the addresses are the same. It will only be true if both values and addresses are equal.

String templates that can introduce additional variables in strings using the $sign:

println("Output a string:" + aString) // HelloWorld
val arg1: Int = 0
val arg2: Int = 1
println("$arg1 + $arg2 = ${arg1 + arg2}") // 0 + 1 = 1
Copy the code

Use $for a single variable and ${} for an expression.

In addition to “”, strings can be raised using “””””” (3 pairs of quotes). 3-quoted strings are more powerful than double-quoted strings, including newlines, and are recognized without the use of \n. At the same time, these escape characters are invalid and can be interpreted as primitive strings.

// Output result:
// \t \n
/ / | ha ha ha
val rawString: String = "" "\ t \ n | ha ha ha "" "
val rawString1: String = "" "$aString  \$aString"" " // HelloWorld \HelloWorld
val rawString2: String = """$ aString""" // $ aString
Copy the code

Triple quoted strings support string templates, that is, they support importing variables with $, and they cannot escape $with \!