官术网_书友最值得收藏!

Numbers

The built-in number types are as follows:

To create a number literal, use one of the following forms:

    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 L suffix and a float value requires a F suffix. The double value, is used as the default for floating-point numbers, and int for integral numbers. The hexadecimal and binary values use the 0x and 0b prefixes, respectively.

Kotlin does not support the 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 value, we can do the following:

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

Similarly, to convert a float value to a double value, we use the toDouble function:

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

The full set of functions for conversions between types is toByte(), toShort(), toInt(), toLong(), toFloat(), toDouble(), and 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; however, they 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 or 0x00001111
val xor = 1 xor 0x00001111 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.

主站蜘蛛池模板: 宁远县| 定南县| 柘荣县| 丰台区| 搜索| 永寿县| 房产| 乃东县| 庄浪县| 宾阳县| 柯坪县| 罗源县| 克什克腾旗| 日照市| 克东县| 佛坪县| 黄梅县| 中卫市| 五河县| 沙雅县| 佳木斯市| 哈巴河县| 宜昌市| 永昌县| 溧水县| 荔波县| 屏东市| 宣城市| 蒲城县| 灵台县| 盐城市| 奉化市| 镇平县| 巫溪县| 玉田县| 娄烦县| 蛟河市| 彭泽县| 团风县| 西丰县| 手游|