A swifter Kotlin to learn--kotlin basic grammar

Source: Internet
Author: User

Just ended the Google IO Conference really dry, as programmers most let me just interested in or Google announced that Kotlin become the first development language Android

This is indeed a more shocking news, as the mobile development of me, is bound to grasp the development of the language in time.

Personally read Kotlin's Chinese document, feel its grammar and swift very similar (in the end who copied who ~), so if you have the effect of swift development of the iOS program, mastering the language is not very difficult! Needless to say, the following is dry:

1. Constants and variables

常量:val a: Int  1val1val//当没有初始化值时必须声明 类型1//赋值变量:var51

(Swift's constant Let, VAR, variable definition is the same ~ ~)

2. Functions

suma: Int, b: Int): Int {     returna + b}

Kotlin language and Swift language, the code does not need to use ";" End

The code above can actually continue to simplify

suma: Int, b: Int)suma + b

The return type of the function is determined by the variable following =

no return value when-unit: equivalent to void

 fun printSum(a: Int, b: Int): Unit {        a

Unit can be omitted

 fun printSum(a: Int, b: Int) {        a

3. Main function

Array<String{}

4. If condition statement

max(a: Int, b: Int): Int {    if(a>b)          returna    else         return b}

Simplified notation

max(aif (aaelse b

5. Use nullable variables and null values to check

Swift has a type of optional
Kotlin also has this type of operation

parseIntString): Int?{}

It is possible for the function to return an int and possibly return null
So null judgment is required for use

Array<String>) {      parseInt(args[0])      parseInt(args[1])      //这里使用x、y容易出现错误,因为x和y有可能是null       ifnullnull){          print(x * y)        }}

Type qualifier is

if(str is String){    //判断类型       returnstr.length}elsereturnnull

Is can be type auto-conversion

fun getStringLength(obj : Any): Int? {      ifis  String){         //obj 将会在这个分支自动转换为String类型               return obj.lengrh      }    //obj 还是Any类型     returnnull}

A swifter Kotlin to learn--kotlin basic grammar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.