1.Kotlin basic Grammar

1.1 First Procedure

fun main(args:Array<String>){
    print("Hello World!")}Copy the code

Hello World!
Copy the code
1.2 constant

val pi=3.1415926
fun main(args: Array<String>) {
    print(pi)
}
Copy the code

3.1415926
Copy the code
1.3 variable

var carCount = 2
var productCount = 1

fun main(args: Array<String>) {
    carCount+=1
    println(carCount)
    productCount++
    println(productCount)
}
Copy the code

3
2
Copy the code

2.Kotlin basic data types

2.1 Basic Numeric types

Kotlin’s basic numeric types include Byte, Short, Int, Long, Float, Double, and so on. Unlike Java, a character is not a numeric type, but a separate data type.

type Bit width
Double 64
Float 32
Long 64
Int 32
Short 16
Byte 8

val aInt: Int = 8
val bInt: Int = 0xFF
val moreInt: Int = 0b00000011
val maxInt: Int = Int.MAX_VALUE
val minInt: Int = Int.MIN_VALUE

val aLong: Long = 1321321
val maxLong: Long = Long.MAX_VALUE
val minLong: Long = Long.MIN_VALUE

val aFloat: Float = 2.0 F
val bFloat: Float = 1E3F
val maxFloat: Float = Float.MAX_VALUE
val minFloat: Float = -Float.MAX_VALUE

val aDouble: Double = 3.0
val bDouble: Double = 3.1415926
val maxDouble: Double = Double.MAX_VALUE
val minDouble: Double = -Double.MAX_VALUE

val aShort: Short = 127
val maxShort: Short = Short.MAX_VALUE
val minShort: Short = Short.MIN_VALUE

val maxByte: Byte = Byte.MAX_VALUE
val minByte: Byte = Byte.MIN_VALUE

fun main(args: Array<String>) {

    println("----------------Int----------------")
    println(aInt)
    println(bInt)
    println(moreInt)
    println(maxInt)
    println(Math.pow(2.0.31.0) - 1)
    println(minInt)
    println(-Math.pow(2.0.31.0))
    println("----------------Long----------------")
    println(aLong)
    println(maxLong)
    println(Math.pow(2.0.63.0) - 1)
    println(minLong)
    println(-Math.pow(2.0.63.0))
    println("----------------Float----------------")
    println(aFloat)
    println(bFloat)
    println(maxFloat)
    println(minFloat)
    println(0.0 F / 0.0 F)
    println("----------------Double----------------")
    println(aDouble)
    println(bDouble)
    println(maxDouble)
    println(minDouble)
    println("----------------Short----------------")
    println(aShort)
    println(maxShort)
    println(minShort)
    println("----------------Byte----------------")
    println(maxByte)
    println(minByte)
}
Copy the code

----------------Int----------------
8
255
3
2147483647
2.147483647 e9
-2147483648
-2.147483648 e9
----------------Long----------------
1321321
9223372036854775807
9.223372036854776 e18
-9223372036854775808
-9.223372036854776 e18
----------------Float----------------
2.0
1000.0
3.4028235 e38
-3.4028235 e38
NaN
----------------Double----------------
3.0
3.1415926
1.7976931348623157 e308
-1.7976931348623157 e308
----------------Short----------------
127
32767
-32768
----------------Byte----------------
127
-128
Copy the code
2.2 Type Safety

/** * Type-safe: once a variable is defined, its type cannot be changed * i.e., a variable cannot be given a value of a different type. * /
fun main(args: Array<String>) {
    var electricBill = 110.9
    electricBill = "20 yuan"
    print(electricBill)
}
Copy the code

Error:(9.20) Kotlin: Type mismatch: inferred type is String but Double was expected
Copy the code
2.3 the Boolean

/** * Boolean :Boolean * indicates a logical "true" or "false" * has only two values, true or false ** /
fun main(args: Array<String>) {
    var isAuth = false // Whether to authenticate
    if (isAuth) {
        println("Certified")
        println("Display authentication page results")}else {
        println("Uncertified")
        println("Show normal surface results")}}Copy the code

Unauthenticated display normal surface resultsCopy the code
2.4 yuan group (a Tuple)

Tuple, assign values to multiple variables simultaneously, divided into pairs and triples.Copy the code


fun main(args: Array<String>) {
    val(Company,employee)= Pair("APPLE"."Jack")
    val(Goods,Num,Price) = Triple("IPhone".1.6888)
    println("The company:${Company}Employee:${employee}Purchased goods:${Goods}Quantity:${Num}Price:${Price}")}Copy the code

Company: APPLE, Employee: Jack Purchased items: IPhone Unit:1Price:6888
Copy the code

    val company = Pair("APPLE"."Jack")
    val goods = Triple("IPhone".1.6888)
    println("The company:${company.first}Employee:${company.second}Purchased goods:${goods.first}Quantity:${goods.second}Price:${goods.third}")
Copy the code

Company: APPLE, Employee: Jack Purchased items: IPhone Unit:1Price:6888
Copy the code
2.5 Nullable type

// Nullable type
// represents the case where the variable may have no value

fun main(args: Array<String>) {

    var addr: String = Chaoyang District, Beijing
    var isVip: Boolean? = null

    if(addr ! =null) {
        println("Your address is${addr}")
    }

    isVip = true
    if(isVip ! =null && isVip == true) {
        println("You are the VIP")}}Copy the code

Your address is Chaoyang District, Beijing and you are VIPCopy the code
2.6 String and Character Types

The type Char

  • The Character corresponds to the Java Character
  • Two bytes, representing a 16-bit Unicode character
  • Characters are used with single quotation marks, for example,’a’,’ middle ‘,’\n’
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

fun main(args: Array<String>) {
    val myJob = "I work as an Android programmer."

    // determine if the string isEmpty :isEmpty, count()
    println(myJob.isEmpty())
    println(myJob.isNotEmpty())
    println(myJob.isBlank())
    println(myJob.count())

    // Define a character with type Char. The value contained in a pair of single quotes is the character word variable, 'b'.
    // Check whether it is a digit or a text
    val aChar = 'a'
    println("Is it words?" + aChar.isLetter())
    println("Is it a number?" + aChar.isDigit())

    // Convert a string to an array of characters using the string toCharArray() method
    for (word in myJob.toCharArray()) {
        println(word)
    }
    // String or character concatenation, can use the + operator
    val myJob2 = "My job is" + 'write' + '作'
    println(myJob2)

    var myJob3 ="My job is driver"
    myJob3 += '! '
    println(myJob3)

    // String template: you can combine variables into a single dynamic variable
    val eventTime = Triple(3.27.3)
    val company1 = "Huawei"
    val company2 = "Millet"
    val newsTitle = "${eventTime.first}month${eventTime.second}Day,${company1},${company2}" +
            "Launch the phone on the same day."
    println(newsTitle)
}
Copy the code

false
true
false
15Text or not:trueIs it a number?falseMy job is A n d r o I d programmeris driver!
3month27Huawei and Xiaomi released mobile phones on The same dayCopy the code