Home » Java » The basic type of Kotlin from entry to pit

The basic type of Kotlin from entry to pit

brief introduction

this blog is mainly to introduce the basic knowledge of Kotlin syntax, to help you quickly grasp the Kotlin, if there are omissions or errors in the message, please. Thank you. series summary address

Get ready

before learning, we have some basic knowledge, as follows:

Kotlin and Java code can be mixed and written, and intermodulation.

can also be used

Kotlin is not written;.

The keyword Val in

Kotlin refers to constants, and VaR stands for variables

Var a:Int =1 - > var template variable name

Kotlin statement:

= value type variable
Basic types

Java we all know the basic types are byte, short, int, long, double, float, Boolean, char, Kotlin are also for the corresponding, but they are more like the packaging of the class java such as int->Integer, Kotlin Int

The following is the correspondence (initial capitalization)

- int->Int

- double->Double

- long->Long

- double->Double

- float->Float

- boolean->Boolean

- char->Char

is an object for everything in Kotlin, that is to say, we can call properties and member variables.

on any variable

Kotlin for digital processing and Java is similar, but slightly different, such as no implicit widening conversion (for Java int type can be converted into long type), the digital representation and the like Java, such as floating point value of 12.8f, but Kotlin does not support 8 hexadecimal values

Storage principle

java platform (Kotlin can be compiled for JavaScript), the default is the use of digital Java primitive types, that is to say Int is in accordance with the int storage. But if we want to use Int? ("?" said may be null, follow-up will explain) or generic, the number is automatically loaded box, that is to say Int according to the Integer.


should note that digital boxing does not guarantee identity, but does preserve equality, for example:

val a: Int = 10000 / / native type storage
Print (a = a) / / the same address, print'true'
Val boxedA: Int = A / / autoboxing?
Val anotherBoxedA: Int = A / / packing again?
Print (boxedA = anotherBoxedA) / / address different print'false'-- does not guarantee the same

look at the following code,

val a: Int = 10000 / / native type storage
Print (a = = a) / / the same content
Val boxedA: Int = a// auto boxing
Val, anotherBoxedA:, Int = = a// again
Print (boxedA = = anotherBoxedA) / / Prints / / the same content equal to'true'--

Here is a simple explanation for "

" and "" = = = = = difference:

= indicates whether the reference the same


"= =" said content is the same, is equivalent to equals (), the value is equal to

Type conversion

supports implicit transformations of expression forms, such as:

var a:Long =1L
Var b:Int=1
The VaR c=a+b //c is a Long

, but not

var a:Long =1L
Var b:Int=a / / compiler error


var b:Int=1
Var a:Long =b / / compiler error

, but each numeric type supports the following conversion, used to explicitly extend numbers, and when you need to convert, you can use the following function.

ToByte (): Byte
ToShort (): Short
ToInt (): Int
ToLong (): Long
ToFloat (): Float
ToDouble (): Double
ToChar (): Char

Kotlin use Char to represent characters, they are not directly as a digital processing (unlike Java). Character. "In parentheses, the use of special characters can be used to support the escape character escape sequence: T, B, N, R, and $.

\ ',".
Boolean value

Kotlin uses Boolean to represent Boolean values, which can hold two types of values, true and false.

The supported operators are as follows:

|| - or short circuit logic
& & - logic and short circuit
- logic non

Kotlin uses the Array class to represent an array, which defines the get and set functions (according to operator overloading, still use internal [] to access) and size properties of
Here we show you how to create an array

creates arrays with arrayOf: VaR, array:, Array = arrayOf (1, b)


arrayOfNulls to create an array of VaR array3:Array = arrayOfNulls (5) / / the specified size and null filling, use the String statement here?, said here the content is null


Array to create an array of ASC: Val = Array (5, I > {(I)} (.ToString * I)) / / in front of the 5 is the specified size, behind I is the corresponding index of 0-4, the initialization of}


Kotlin has no packing overhead special classes to represent the primitive type: ByteArray, ShortArray, IntArray array and so on. These classes and Array did not inherit relationship, but they have the same attribute set. They also have the corresponding.

factory method
Character string

Kotlin, strings are represented by String, and strings are immutable. You can use s[i] to access characters in them, or you can walk through strings to access characters in them.

There are two kinds of strings in Kotlin, escape strings and native strings (which cannot be escaped characters). Here's an introduction:

Escape string: this string similar to Java, Val, a:String=, test, word, n"

native string:

// up and down the three "" "quotes" package, you need to display content
Val a:String=
Baiyun black soil
The universe
"This will be the same output / string format, keep here

String template

string can contain a string template, which is a small piece of code that merges the result of the evaluation into a string. The template expression uses the $US symbol, and the following example illustrates:

val a=10
Val str= "a value is: $a / output: a content is: 10

can also be used in methods such as

val a= "test""
Val str= "a content is $a, the length is ${a.length} / output: a content is test, length is 4


string template supports native strings and escape strings.


so far has learned the basic type of Kotlin related knowledge, review more thinking, continue , follow-up content, .