变量、常量、整数、浮点数、操作符、元组、包、导入
Book.kt
kt
package com.wujialiang.pack
class Book {
var title: String = "Hello"
}
val PI = 3.14;
val E = 2.178;
Main.kt
kt
//引入包
//import com.wujialiang.pack.Book;
import com.wujialiang.pack.*;
//重命名导入
import com.wujialiang.pack.Book as TestBook;
fun addPIAndE()=PI+E;
fun main() {
//常量
val constant = "我是常量";
//constVar ="我要改变";
println(constant);
//变量
var variable = "我是变量";
println(variable);
variable = "我能改变";
println(variable);
//整数类型
val int: Int = 1;
println(int);
val long: Long = 1L;
println(long);
val double: Double = 1.0;
println(double);
val float: Float = 1.0f;
println(float);
val octal: Int = 0xACF;
println(octal);
val hexadecimal: Int = 0b01010111;
println(hexadecimal);
//类型转换
val intToLong = int.toLong();
println(intToLong);
val intToChar = 97.toChar();
println(intToChar);
//左移 1左移2
val result1 = 1 shl 2;
println(result1);
//右移 1右移2
val result2 = 1 shr 2;
println(result2);
//无符号右移
val result3 = 1 ushr 2;
println(result3);
//与或非异或
val result4 = 1 and 0x00001111;
println(result4);
val result5 = 1 or 0x00001111;
println(result5);
val result6 = 1 xor 0x00001111;
println(result6);
//取反
val result7 = 1.inv();
println(result7);
val pi = 3.1415926;
println("圆周率为${pi}");
val bool:Boolean = false;
println(bool);
//元组
val grade = Triple("一级","二级","三级");
println("${grade.first}-${grade.second}-${grade.third}");
//可空类型
var address:String? = null;
address="济南市";
println(address);
var a =3;
a = -100;
println(+a)//-100
println(-a)//100
//操作符 + - * / % -= += *= /= > < >= <= ==
var r=1;
r /=1;
println(r);
//import导入和重命名
val result8 = addPIAndE();
println(result8);
val book = TestBook();
println(book.title);
}