Mat*_*any 231 constants kotlin
我最近读过这个const关键字,我很困惑!我找不到它和const关键字之间的任何区别,我的意思是我们可以使用它们来制作一个不可变的变量,还有什么我不知道的吗?
Luk*_*itz 323
consts是编译时常量.这意味着它们的值必须在编译期间分配,与vals 不同,它可以在运行时完成.
这意味着,consts永远不能分配给函数或任何类构造函数,而只能分配给String或原语.
例如:
const val foo = complexFunctionCall() //Not okay
val fooVal = complexFunctionCall() //Okay
const val bar = "Hello world" //Also okay
Run Code Online (Sandbox Code Playgroud)
小智 18
const kotlin 到 Java
const val Car_1 = "BUGATTI" // final static String Car_1 = "BUGATTI";
Run Code Online (Sandbox Code Playgroud)
val kotlin 到 Java
val Car_1 = "BUGATTI" // final String Car_1 = "BUGATTI";
Run Code Online (Sandbox Code Playgroud)
用简单的语言
示例 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"
}
Run Code Online (Sandbox Code Playgroud)
这是因为 getCar() 在运行时被评估并将值分配给 Car。
此外 -
Jin*_*ang 15
您可以将Kotlin转换为Java.然后你可以看到const有一个比val更多的静态修饰符.像这样的简单代码.
科特林:
const val str = "hello"
class SimplePerson(val name: String, var age: Int)
Run Code Online (Sandbox Code Playgroud)
致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;
}
}
Run Code Online (Sandbox Code Playgroud)
Aja*_*rma 15
在科特林,const并且val两者代表的不变性和只读值,并作为final在Java关键字。
val关键字必须用于声明运行时值,const关键字必须用于声明编译时值。
请记住, const 必须仅用于原始数据类型,而不用于函数和构造函数。
例子 -
const val fun1 = anyFunctionOrConstructor() // it is not fine
val fun2 = anyFunctionOrConstructor() // it is perfectly fine
const val aa = "My String" // it is perfectly fineRun Code Online (Sandbox Code Playgroud)
Kif*_*lah 12
这两个val和const是不可变的。
const用于声明编译时常量,而val用于声明运行时常量。
const val VENDOR_NAME = "Kifayat Pashteen" // Assignment done at compile-time
val PICon = getIP() // Assignment done at run-time
Run Code Online (Sandbox Code Playgroud)
小智 12
因为我读了很多,“val”意味着不可变:这绝对不是这种情况,只需看这个例子:
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
}
Run Code Online (Sandbox Code Playgroud)
遗憾的是,真正的不变性目前只能通过 const 来实现——但这只能在编译时实现。在运行时你无法创建真正的不变性。
val 只是意味着“只读”,您不能直接更改此变量,只能间接更改,就像我在上面的示例中所示的那样。
与Kotlin关键字相比,Kotlinval关键字用于只读var属性。read-only属性的另一个名称是immutable。
科特林代码:
val variation: Long = 100L
Run Code Online (Sandbox Code Playgroud)
Java 等效项如下所示:
final Long variation = 100L;
Run Code Online (Sandbox Code Playgroud)
我们const也使用关键字来表示不可变属性。const用于编译时已知的属性。这就是区别。考虑到const财产必须申报globally。
Kotlin 代码(在操场上):
const val WEBSITE_NAME: String = "Google"
fun main() {
println(WEBSITE_NAME)
}
Run Code Online (Sandbox Code Playgroud)
Java代码(在操场上):
class Playground {
final static String WEBSITE_NAME = "Google";
public static void main(String[ ] args) {
System.out.println(WEBSITE_NAME);
}
}
Run Code Online (Sandbox Code Playgroud)
对于那些正在寻找val和之间哪个更合适或更有效的人const:
对于 String 或任何原始数据类型,const val建议使用而不是val. 因为val将在运行时知道,所以当您的应用程序运行时它将处理所有值。另一方面,const val将在编译时更早地执行此操作。因此,性能方面const val会给出更好的结果。
让我们通过一个例子来学习这一点。
object Constants {
val NAME = "Amit"
}
Run Code Online (Sandbox Code Playgroud)
注意:我们没有使用const.
并且,我们按如下方式访问它NAME:
fun testValWithoutConst() {
val name = Constants.NAME
}
Run Code Online (Sandbox Code Playgroud)
现在,我们需要反编译这段代码。为此,我们必须将此 Kotlin 源文件转换为 Java 源文件。
我们将得到以下输出:
public final void testValWithoutConst() {
String name = Constants.INSTANCE.getNAME();
}
Run Code Online (Sandbox Code Playgroud)
输出如预期。
上面的例子没有关键字const。现在,让我们使用const关键字。
为此,我们将Constants在 Kotlin 中修改我们的对象类,如下所示:
object Constants {
const val NAME = "Amit"
}
Run Code Online (Sandbox Code Playgroud)
注意:我们正在使用const.
并且,我们按如下方式访问它NAME:
fun testValWithConst() {
val name = Constants.NAME
}
Run Code Online (Sandbox Code Playgroud)
现在,当我们反编译这段代码时,我们将得到以下输出:
public final void testValWithConst() {
String name = "Amit";
}
Run Code Online (Sandbox Code Playgroud)
在这里,我们可以看到该变量NAME已被其值替换,即Amit。
由于该值已被内联,因此在运行时访问该变量不会产生任何开销。因此,它将带来更好的应用程序性能。
这就是在Kotlin中使用的优点const。
参考我的博客:Advantage of using const in Kotlin
| 归档时间: |
|
| 查看次数: |
33439 次 |
| 最近记录: |