kotlin基础

我们可以从总体上学习一下kotlin的基础:

Vals and Vars

Kotlin存在两个定义的关键字,val和var.var用来定义可变变量,即你可以通过赋值来改变它,这就和java是一样的。此外,var也可以延迟初始化:

1
2
3
val myname = "kotlin"
var name: String
name = "kotlin"

使用var定义的变量可以重新赋值,毕竟它们是可变的。

1
2
var name = "kotlin" 
name = "more kotlin"

val用来定义只读变量.这和在java中使用final来定义常量是一个效果,所以一个val刚开始创建时就要初始化,而且之后是不能再次进行修改的。

1
val name = "kotlin"

一个只读变量并不意味着这个实例本身是不可变的。
A read only variable does not mean the instance itself is automatically immutable. The instance may still allow its member variables to be changed via functions or properties, but the variable itself cannot change its value or be reassigned to another value.

Basic types

One of the big changes in Kotlin from Java is that in Kotlin everything is an object. If you come from a Java background, then you will already be aware that in Java there are special primitive types which are treated differently from objects. They cannot be used as generic types, do not support method/function calls, and cannot be assigned null. An example is the primitive type boolean.
Kotlin与java最大的不同就是kotlin是真的万物皆对象,java的基础类型(primitive types)则不是对象,他们不能当成普通类型使用,本身不支持方法功能的调用,而且不能赋值为null.比如基础类型boolean.

Java introduced wrapper objects to offer a work around in which primitive types are wrapped in objects, so that java.lang.Boolean wraps a boolean in order to smooth over the distinctions. Kotlin removes this necessity entirely from the language by promoting the primitives to full objects.

java引进了包装类来提供原始类型和对象之间的转换。所以

Whenever possible, the Kotlin compiler will map basic types back to JVM primitives for performance reasons. However, sometimes the values must be boxed, such as when the type is nullable, or when it is used in generics. Boxing is the conversion from a primitive type to a wrapper type that types place whenever an object is required but a primitive is presented.

Numbers

Type Width
Long 64
Int 32
Short 18
Byte 8
Float 32

你可以这样创建一个Number:

1
2
3
4
5
6
val int = 123
val long = 123456L
val double = 12.34
val float = 12.34F
val hexadecimal = 0xAB
val binary = 0b01010101

You will notice that a long value requires the suffix L and a float, the suffix F. The double is used as the default for floating point numbers, and int for integral numbers. The hexadecimal and binary use the prefixes 0x and 0b respectively.
你会注意到long类型会需要加上后缀L,float需要加上F.double 是默认的浮点型数字,int表示整型。
hexadecimal和二进制要使用前缀0x或0b.

Kotlin does not support automatic widening of numbers, so conversion must be invoked explicitly. Each number has a function that will convert the value to one of the other number types. For example to convert from an integer to a long we can do the following.

Kotlin不支持强制转换,所以命名时要精确。每一个number都有一个函数来方便抓成其他的 number类型。比如我们把一个int值转成Long:

1
2
val int = 123
val long = int.toLong()

与此类似,我们可以使用doDouble函数来把一个float值转成double值。

1
2
val float = 12.34F 
val double = float.toDouble()

The full set of functions for conversions between types is toByte(), toShort(), toInt(), toLong(), toFloat(), toDouble(), toChar().

The usual bitwise operators – left shift, right shift, unsigned right shift, logical and, logical or and exclusive logical or – are supported by Kotlin. Unlike Java, these are not built in operators but named functions instead but can still be invoked like operators:

val leftShift = 1 shl 2 val rightShift = 1 shr 2 val unsignedRightShift = 1 ushr 2

val and = 1 and 0x00001111 val or = 1 or0x00001111 val xor = 1 xor0x00001111 val inv = 1.inv()

Notice that inverse is not a binary operator, but a unary operator and so is invoked using the dot syntax on a number.