Kotlin grammar learning (a)

Basic grammar

Defined Functions

  • java
public int method(int a,int b){
    // todo
    return a+b;
}
复制代码
  • kotlin
fun method(a:Int,b:Int):Int{
    return a+b
}
复制代码
  • In the kotlin everything is an object, the basic data types (int, long, boolean, double , short, float, byte) java be used in its corresponding package in kotlin class (Int, Long, Boolean, Double, Short, Float, Byte) . Note : 1, the number of types can not be installed for implicit in Kotlin (example: java in type long int) 2, when Kotlin function does not return a value, as Unit Int replaced, omitted or

Define the variable

  • java
private final int a;
public int b=1;
String c;
复制代码
  • kotlin
private val a:Int?=1
public val b=1
lateinit var c:String
private val d:Int by lazy { 1 }
复制代码
  • In kotlin declared with var and val Variables and Constants
  • Note: 1, under normal circumstances, when you declare a variable must be initialized 2, if you want to add variables need to be null after the type declaration '? '3, if we are not initialized when they are declared, you can add keywords before var lateinit (Val does not support lateinit) 4, val can be used by lazy key (initialization at its first call) at initialization time delay

And using null null detector

  • java
String a=null;
if(a!=null){
    int size=a.length();
}
复制代码
  • kotlin
var a:Strng?=null
var size=a?.length
var size2=a!!.length
复制代码
  • When using the parameter may be empty in kotlin in need '? 'Determines whether the current parameter is empty, if empty'? After the logic 'will not be executed. You may also be used '!' To determine whether the air, but which parameter is directly empty null pointer exception is thrown.

Usage type detector and automatic conversion

  • java
public String get(Object obj){
    if(obj instanceof String){
        String s=(String)obj;
        return s;
    }
    return null;
}
复制代码
  • kotlin
fun get(obj:Any):String?{
    if(obj is String){
        return obj
    }
    return null
}
复制代码
  • by kotlin is to determine whether the type of the front back type instance; if a local variable or non-variable properties has judged that a certain type, then the branch after the detection of this type can be used as directly, without displaying the conversion, For example: obj code above.
  • Note: kotlin in Any java equivalent of Object is the parent of all classes.

Use a for loop

  • java
public void test(List<Stirng> list){
    int size=list.size;
    for(int i=0;i<size;i++){
        System.out.println("list 第"+i+"个数据:"+ list.get(i));
    }
}
复制代码
  • kotlin
fun test(list:ArrayList<String>){
    for(item in list){
        println("list item:$item")
    }
    
    for(item in list.indices){
        println("list 第$item 个数据:${list[item]}")
    }
    
    for((index,item) in list.withIndex()){
            println("list 第$index 个数据为:$item")
        }
}
复制代码
  • By kotlin in keyword through the data, the code above, the first for loop out directly in the data list; second for loop index list is taken out by the index data can be extracted; the third loop may be removed for simultaneously indexing and data traversal.

When using expressions

  • java
public void test(int a){
    switch(a){
        case 1:
        // todo
            break;
        case 2:
        // todo
            break;
        default:
        // todo
            break;
    }
}
复制代码
  • kotlin
fun test(a:Int){
    when(a){
        0,1->{
            //todo
        }
        2->{
            //todo
        }
        in 3..10->{
            //todo
        }
        else{
            //todo
        }
    }
}
复制代码
  • when expressions of java kotlin in similar switch.
  • When a plurality of conditions are to perform the same logic, it can be separated with commas;
  • For the range of conditions can be used in keywords in in with the former '! 'Indicates negation;

(Range) range

  • kotlin
fun test(a:Int){
    //检测一个数是否在某个范围
    if(a in 1..10){
        println(x)
    }
    
    //检测某个数是否在区间之外
    var list=listOf(2,3,4,5)
    if(-1 !in 0..list.lastIndex){
        println("-1 不在 list范围内!")
    }
    
    //区间迭代
    for(x in 1..10){
        println(x)
    }
    
    for(x in 1..10 step 2){
        prinlnt(x)
    }
    
    for(x in 9 downTo 0 step 3){
        println(x)
    }
    
}
复制代码
  • kotlin section may be used in x..y expressed in the sense that the number x between y and x comprising x and y <y, if x> y using downto ;
  • step x represents plus or minus per iteration number x;

Set of arrays

  • statement
//声明空数组
var list= arrayOfNulls<Any>(2)
//声明有初始值的数组
var list1= arrayOf(1,2,3,4)

//声明List
var list=ArrayList<String>()
//声明List并且初始化 
var list3= listOf(1,2,3)

复制代码

Reproduced in: https: //juejin.im/post/5d06eb146fb9a07ed36ea985

Guess you like

Origin blog.csdn.net/weixin_33725807/article/details/93180023