Define variables, constants

In Kotlin, you must use the var and val keywords to define variables

Define variables: Variables can be read and written, and can be assigned repeatedly after definition, which is equivalent to ordinary variables in Javavar< variable identifier >: < data type > = < initial value > Define constant: a constant can be read but not written. Once defined, its value cannot be modified, equivalent to JavafinalModified constantval< constant identifier >: < data type > = < initialization value >Copy the code
var name: String = "kotlin"	// Assign directly when declaring
var age: Int 	 // Data types cannot be omitted without initializing values
age = 100		// the value is assigned after the first statement
var sex = 1		// Automatically infer data type Int

val PI: Double = 3.1415 // Declare constants directly assigned
// if PI = 3.20, a syntax error will be reported
val title: String		// Declare a constant
title = "kotlin NB"	// Unassigned constants can be assigned once
Copy the code

Declaring nullPointExceptions in Java doesn’t matter if a variable is null, just add a non-null determination when it is used. Otherwise, a NullPointException will appear, which may add useless code invisibly. In Kotlin we can avoid such useless code judgments. We can declare whether a variable can be null(nullable) as the case may be. If the variable cannot be null, we can use the formal definition we used for ordinary variables above.

Nullable variable declaration format:var< variable identifier >: < data type >? = < initial value >/null

var name: String? = null
var title: String? = "kotlin"
var age: Int?
Copy the code

Note: Nullable variable declaration when the data type cannot be omitted, and the data type must be added after? To indicate that a variable can be null

A constant modified by const

Const can only modify constants declared by val, not variables declared by var

A const modifier constant can only be in the top layer of an object declaration, or in a companion object declaration

Constants declared by const must be assigned directly

Properties declared by const without qualifiers default to public. Property qualifiers declared by val only default to private

annotation

The Kotlin language, like the Java language, supports writing comments inside programs to improve code readability

  • Single-line comments (to//At the beginning)
 // This is a one-line comment
Copy the code
  • Multi-line comments (block comments to/ *At the beginning,* /At the end)
/* Multi-line comment code block 1 code block 2 */
Copy the code

Unlike Java, Kotlin’s multi-line comments can be nested. The Java language does not support nested multi-line comments


class Person{
// The following nested comments are incorrect in Java and correct in Kotlin
	/*public String name; /*public int sex; * /
	 public int age; 
	 */
}

Copy the code
  • Class annotation, method annotation (to/ * *At the beginning,* /The end, syntax is the sameJavaThe same)
Fun add(a: Int, b: Int): Int {return a + b}Copy the code