const val str = "hello"
class SimplePerson(val name: String, var age: Int)
Java(部分):
@NotNull
public static final String str = "hello";
public final class SimplePerson {
@NotNull
private final String name;
private int age;
@NotNull
public final String getName() {
return this.name;
}
public final int getAge() {
return this.age;
}
public final void setAge(int var1) {
this.age = var1;
}
public SimplePerson(@NotNull String name, int age) {
Intrinsics.checkParameterIsNotNull(name, "name");
super();
this.name = name;
this.age = age;
}
}
const val Car_1 = "BUGATTI" // final static String Car_1 = "BUGATTI";
val kotlin致爪哇
val Car_1 = "BUGATTI" // final String Car_1 = "BUGATTI";
简单地说
const变量的值在编译时已知。
val的值用于在运行时定义常量。
示例1 -
const val Car_1 = "BUGATTI" ✔
val Car_2 = getCar() ✔
const val Car_3 = getCar() ❌
//Because the function will not get executed at the compile time so it will through error
fun getCar(): String {
return "BUGATTI"
}
class Test {
var x: Int = 2
val y
get() = x
}
fun main(args: Array<String>) {
val test = Test()
println("test.y = ${test.y}") // prints 2
test.x = 4
println("test.y = ${test.y}") // prints 4
}