Kotlin Basic Syntax

Source: Internet
Author: User

Before you read the following, you may need to understand some of the features of the Kotlin language: At the end of a statement, you can declare a method with the Fun keyword, and if the function is a method that overloads the parent class, you must also add the Override keyword. The parameter of the method is the first-write parameter name followed by the colon-and-write parameter type; The Kotlin source file does not need to match the directory and package, and the source file can be placed in any file directory. But when we write classes such as Android activity, the declaration in the manifest file must match the actual package path. Defining function Methods

Example 1: The method contains two int parameters and returns an int type value

Fun sum (A:int, b:int): Int { 
    return a + b
}

Example 2: The method body has only one statement and automatically guesses the return type

Fun sum (a:int, b:int) = a + b

Example 3: If the method is public, you must explicitly write out the return type

Public fun sum (A:int, b:int): Int = a + b

Example 4: Return a meaningless value (similar to void in Java)

Fun Printsum (A:int, b:int): unit { 
    print (A + b)
}

//If the unit type is returned, it can be omitted (as is the case for the public method): Public fun
Prin TSum (A:int, b:int) { 
    print (A + b)
}
use nullable value and null value detection

The reference or function return value must be explicitly labeled nullable if it is possible to be a null value. (A question mark after the type indicates that the object may be empty, with two exclamation points indicating that the type must not be empty)

Fun Main (args:array<string>) { 
  if (args.size () < 2) {
    print ("both integers expected")
    return
  }

  val x = parseint (Args[0])
  val y = parseint (args[1])

  //must be judged because X or Y has the potential to be empty
  if (x! = null && y! = null) {
    //x and Y are automatically converted to non-null type
    check print (x * y)
  } 
}

/**
 * If Str cannot be converted to int type. Returns null
 *
/Fun parseint (str:string): Int? { 
  //(code slightly)
}
in keyword usage

If a number is within a range, you can use the IN keyword

Print Y-times OK if
(x in 1..y-1) 
  print ("OK")

//If x does not exist in array, output out
if (x!in 0..array.lastindex) 
  Print ("Out")

//printing 1 to 5 for
(x in 1..5) 
  print (x)

//Traversal collection (similar to for (String Name:names) in Java) for
(name in Names)
  println (name)

//If the Names collection contains a text object, print Yes if
(text in Names)
type detection and automatic conversion

is keyword usage (similar to the instanceof keyword in Java)
Example 1

Fun Getstringlength (obj:any): Int? {
  if (obj is String) {
    //after type judgment, obj is automatically converted to String type
    return obj.length 
  }

  ///There is a method here, Unlike instanceof in Java, use!is
  //if (obj!is String) {////   XXX
  //}//

  obj Here is still a reference of any type
  return Null
}

Example 2

Fun Getstringlength (obj:any): Int? {
  //on the left obj has been judged to be of type string, so the right side of && can be directly using obj as a string type to use
  if (obj is string && obj.length > 0) {
    return obj.length 
  }
  return null
}
When expression

(similar to switch in Java)

Cases (Obj:any) {when 
  (obj) {1--       print ("first item")
    "Hello", print ("This is string Hello") is
    Long, print ("This is a long type of data")
    !is String, print ("This is not a string type of data")
    else,    print (" else similar to default in Java ")
  }
}
Related Article

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.