欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

kotlin 的基本数据类型

程序员文章站 2024-03-14 22:57:29
...

 

Kotlin 的基本数值类型包括 Byte、Short、Int、Long、Float、Double 等。不同于Java的是,字符不属于数值类型,是一个独立的数据类型。

类型 位宽度
Double 64
Float 32
Long 64
Int 32
Short 16
Byte 8

 


/**
 * Created by jingwen.
 */
fun main(args:Array<String>){

    //整型
    var age:Int = 12;
    var direct:Byte = 1;
    var band:Short = 1;
    var salary:Long = 44433333;

    //浮点型
    var zhuSalary:Float = 5223.4f;
    var famlySalary:Double = 112424422443.0

    //boolean
    var isBoy:Boolean = true


    var  char:Char = 'a';
    var  name:String = "hello jingwen"

    println("age ="+age)
    println("direct ="+direct)
    println("band ="+band)
    println("salary ="+salary)

    println("isBoy ="+isBoy)

    println("zhuSalary ="+zhuSalary)
    println("famlySalary ="+famlySalary)

    println("char ="+char)
    println("name ="+name)



    //最大值和最小值
    var  intMax:Int = Int.MAX_VALUE;
    println("int max:")
    println(intMax)
    println(Math.pow(2.0,31.0)-1)

    println("int min")
    var intMin:Int = Int.MIN_VALUE;
    println(intMin)


    println(-Math.pow(2.0,31.0))


    println("float max")
    var floatMax = Float.MAX_VALUE
    println(floatMax)

    println("float min")
    var floatMin = -Float.MAX_VALUE
    println(floatMin)
    println(Float.MIN_VALUE)

    //非数值,kotlin独有的
    println(0.0f/0.0f)


    //类型转换必须是显示转换
    var a:Int = 12;
    var b:Byte = a.toByte();


    var  c:Long = b.toLong();

    //字符串模板
    var num1:Int = 1
    var num2:Int = 2
    //java普通方式
    println(""+num1 +"+"+num2+"="+(num1+num2))
    //kotlin 模板
    println("$num1+$num2=${num1+num2}")

    //"""

    var string:String = """
    jingwen
    zhu
    \t
    \n
    cc
    """

    println(string)
    println("字符的个数 ${string.length}")

    //字符串的比较
    var  s1:String = String(charArrayOf('a','b','c','d'))
    var  s2:String = "abcd"
    println(s1==s2)//public open operator fun equals(other: Any?): Boolean
    println(s1===s2)//判断是否同一个对象





}

运行结果:

age =12
direct =1
band =1
salary =44433333
isBoy =true
zhuSalary =5223.4
famlySalary =1.12424422443E11
char =a
name =hello jingwen
int max:
2147483647
2.147483647E9
int min
-2147483648
-2.147483648E9
float max
3.4028235E38
float min
-3.4028235E38
1.4E-45
NaN
1+2=3
1+2=3

    jingwen
    zhu
    \t
    \n
    cc
    
字符的个数 46
true
false