当前位置:网站首页>Instructions for common symbols in kotlin

Instructions for common symbols in kotlin

2022-07-28 15:06:00 Guyu γ










 Guyu


  • Kotlin What is it? ?

Kotlin [ Colin ] Is a static programming language for modern multi platform applications , from JetBrains Development ;
Kotlin It's a static language , Support multiple platforms , Including mobile end 、 Server and browser ;
Kotlin It is also a language that integrates object-oriented and functional programming , Supports generics 、 Safe empty judgment , also Kotlin And Java Complete interaction can be achieved .

stay Google I/O 2017 in ,Google announce Kotlin Become Android Official development language .




Kotlin The role of :

① concise : Greatly reduce the number of template code .
② Security : Avoid errors of the whole class such as null pointer exceptions .
③ Interoperability : make the best of JVM、Android And the browser's existing library .
④ Tool friendly : You can use any Java IDE Or use the command line to build .




Kotlin Some commonly used compound symbols ( ‘?’ ‘?:’ ‘!!’ ‘::’ ‘. .’ ‘as?’ )




1、 [ : ] ------ The colon The operator

" : "

Operators are used to define variables 、 Class inheritance, etc

" : " Colon and reference type .


  • " : " Operators are used to define variables 、 Class inheritance, etc

// Defining variables 
var name: String
// Inheritance class 
class MainActivity : AppCompatActivity()





2、 [ ? ] ------ question mark

" ?"

Decorated after the type of member variable , Represents this variable Can be null (null), Under no circumstances will the system report its null pointer exception ;
" ? " The modification represents the object after the object if it is empty (null), Then the following logic will not be processed ;

If the variable we define is a type that can be empty , It is necessary to use 【 String? 】.

 Guyu

  • " ?" Indicates that this object can be empty

// Add... After the variable type  ? , It means that the variable can be null 
var age: String? = "23" 

//  If  str  It can't be turned into  Int  type , Then return to  null
fun parseInt(str: String): Int? {
     
 // nothing
}




3、 [ !! ] ------ Double sense exclamation mark The operator

" !! "

After the object or method passing arguments , Express Can't be empty (null), If null Throw an exception ;
Kotlin Non null assertions are not recommended , Usually we use them " ?: " To prevent the program from crashing when it runs with null pointer exceptions ;

" !! " The usage of is equivalent to Java Inside if else Judge whether it is null.

 Guyu


  • " !! " Null Check the mechanism symbol

// Non empty  b, If  b  It's empty , Throw a null pointer 
val l = b!!.length




4、 [ ?: ] ------ Exclamation mark colon The operator (Elvis)

" ?: "

Nullable variables can be specified It's empty time , Call the return value of the member method or property in the variable ;
Its grammatical form is : “ expression A ?: expression B”.
If the left expression A Non empty , Returns the left expression A Value , Otherwise return the right expression B Value . If and only if the left side is empty , For the right expression B evaluation .

" ?: " The usage of is similar to Java Use the trinocular operator .

 Guyu


  • " ?:" If it is not empty, use , Otherwise, the specified value is returned

//a  Not empty , return  a  The length of , Otherwise return to  -1
val 1ength==a?.length?:-1
// amount to 
val length:Int=if(a!=null)a.length else -1




5、 [ :: ] ------ Double colon The operator

" :: "

Means to treat a method as a parameter , Pass to another method for use , Generally speaking, it refers to a method ; Can be used to get the class Class object ;

" :: " Double colon and method name The function is to pass the method as a parameter .


  • " :: " Can be used to get the class Class object

// obtain  SecondActivity  object 
startActivity(Intent(this@MainActivity, SecondActivity::class.java))





6、 [ as ] ------ Type conversion operators

" as "

Indicates if the type conversion is unsuccessful , Type conversion operators usually throw an exception , So unsafe type conversions Using operators as ;
When the type conversion fails, it will return null, To avoid throwing exceptions , You can use safe type conversion operators as?;

" as " If you convert a value to a given type , If the type is not appropriate, return null.

 Guyu





7、 [ . . ] ------ Double dot Interval operator

" . . "

Indicates the interval ( Or range ) The expression is in the form of an operator … Of rangeTo The function is supplemented by in and !in formation .

in The operator , representative i In a certain interval class ;
!in representative i Not in a certain interval class ;
. . Closed range operators , On behalf of the a To b Interval class , contain a and b;
until Semi closed interval operator , representative a To b Section , contain a It doesn't contain b;
downTo() function : Loop in flashback interval ,a To b Interval flashback iteration number ;
step() function : Jump function , Parity decision choice , You can specify any step .


//  Output number  “ 1  To  10 ”
if (i in 1..10) {
     print(i)} 	
//  Output number  “ 1  To  9 ” Same as [1,10)
for (i in 1 until 10) {
     print(i)} 	
//  Output number  “54321”
for (i in 5 downTo 1) print(i) 
//  Output number  “ 135 ”
for (i in 1..5 step 2) {
     print(i)} 	
//  Output number  “ 531 ”
for (i in 5 downTo 1 step 2) {
     print(i)} 	



//  If  i  stay  1  To  10  Between ten numbers , It outputs  i
if(i in 1..10){
     
    print (i)
}
//  If  i  be not in  1  To  10  Between ten numbers , It outputs  i
if(i !in 1..10){
     
    print (i)
}
//  Output nothing , Pay attention to this decreasing situation, and it is recommended to use  until  or  downTo 
for (i in 4..1){
    
	 print(i)
 }
//  If  i  stay  1  To  10  Between nine numbers ( It doesn't contain  10), It outputs  i
if(i in 1 until 10){
     
    print (i)
}





8、 [ -> ] ------ The middle crossbar is larger than The operator

" -> "

From the perspective of morphology , It's a kind of Flow direction and corresponding The relationship between ;
Indicates that after the execution of the previous statement , The execution flows to the statement pointed to , And it corresponds to .





9、 [ == and === ] ------ Equal sign The operator

== Judge value Whether it is equal or not , Compare the two The number size .
=== Judge Value and reference Is it completely equal , Compare the two object Address .



var a:Int = 10
print(a===a)	// result :true
var b:Int =a
var c:Int =a
print(b===c)	// result :false
print(b==c)		// result :true





10、 [ is ] ------ Belongs to the operator

" is "

This operator can be used to determine whether an instance Belong to Another example



//name  Whether it is  String  type 
if (name is String){
     
    print(true)
}





11、 [ $ ] ------ Template operator

" $ "

This template operator can Output A variable value , Equivalent to concatenating strings .



//  The output is  My name is GuYu
var name="GuYu"
print("My name is $name")





Kotlin Document to .kt For the suffix ; Its basis is The mind map is shown in the figure below :
 Guyu


  • Recommended reference links :

    https://www.cnblogs.com/shen-hua/category/1141903.html
    https://jetictors.github.io/categories/Kotlin/page/2/










Note:
Welcome to thumb up , Leaving a message. , Reprint please give the link of the original text in the obvious position of the article page
The knower , Thank you for reading my article in the vast crowd
No personality Where did you get the signature !
Please pay attention to Am I
Ongoing update



scan Have a pleasant surprise !
2021 09 - Guyu.com | 【 copyright Infringement must be investigated 】
原网站

版权声明
本文为[Guyu γ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/196/202207130916538358.html