[英]How is val in scala different from const in java?
Anyone care to elaborate on how val in scala is different from const in java? 任何人都在关注scala中的val与java中的const有何不同?
What are the technical differences? 有哪些技术差异? I believe I understand what "const" is in c++ and java. 我相信我理解c ++和java中的“const”是什么。 I get the feeling that "val" is somehow different and better in some sense but I just can't put my finger on it. 我觉得“val”在某种程度上是不同的和更好的,但我不能把手指放在它上面。 Thanks 谢谢
const
in Java has no function —it's reserved but you can't actually use it for anything. Java中的const
没有函数 -it是保留的,但实际上你无法使用它。 Declaring a Java variable as final
is roughly equivalent . 将Java变量声明为final
是大致相同的 。
Declaring a variable as a val
in Scala has similar guarantees to Java final
—but Scala val
s are actually methods unless they're declared as private[this]
. 在Scala中将变量声明为val
具有与Java final
类似的保证 - 但Scala val
s实际上是方法,除非它们被声明为private[this]
。 Here's an example: 这是一个例子:
class Test(val x: Int, private[this] val y: Int) {
def z = y
}
Here's what the compiled classfile looks like: 这是编译的类文件的样子:
$ javap -p Test
Compiled from "Test.scala"
public class Test {
private final int x;
private final int y;
public int x();
public int z();
public Test(int, int);
}
So it's clear from this example that private[this] val
is actually Scala's equivalent of Java's final
in that it just creates a field (no getter method). 所以从这个例子可以清楚地看出, private[this] val
实际上是Scala相当于Java的final
,它只是创建了一个字段(没有getter方法)。 However, it's a private field, so even that's not quite the same. 然而,它是一个私人领域,所以即便如此也是如此。
Another fun fact: Scala also has a final
keyword! 另一个有趣的事实: Scala也有一个final
关键字! Scala's final
behaves similarly to how final
works for classes in Java—ie it prevents overriding. Scala的final
行为类似于Java中类的 final
工作方式 - 即它可以防止覆盖。 Here's another example: 这是另一个例子:
final class Test(final val x: Int, final var y: Int) { }
And the resulting class: 由此产生的课程:
$ javap -p Test
Compiled from "Test.scala"
public final class Test {
private final int x;
private int y;
public final int x();
public final int y();
public final void y_$eq(int);
public Test(int, int);
}
Notice that the final var
definition makes the getter and setter methods final (ie you can't override them), but not the backing variable itself. 请注意, final var
定义使得getter和setter方法最终(即你不能覆盖它们),而不是后备变量本身。
A Scala val
is equivalent to a final
variable or field in Java. Scala val
等同于Java中的final
变量或字段。 A Scala var
is equivalent to a non- final
variable or field in Java. Scala var
等同于Java中的非final
变量或字段。 (By the way, neither "var" nor "const" are Java terms.) (顺便说一句,“var”和“const”都不是Java术语。)
The aspect that's "better" about Scala's syntax choice to use val
and var
is that code using non-modifiable values is generally easier to understand. 关于Scala使用val
和var
的语法选择“更好”的方面是使用不可修改值的代码通常更容易理解。 In Java, final
is "syntactic vinegar", and style guides tend to argue over whether code should use final
to encourage better coding or omit final
to avoid the clutter. 在Java中, final
是“syntactic vinegar”,样式指南倾向于争论代码是否应该使用final
来鼓励更好的编码或省略final
以避免混乱。 Scala doesn't have this conundrum because the var
and val
are exactly the same length, so you're a bit more free to just choose the one that makes the most sense. Scala没有这个难题,因为var
和val
的长度完全相同,所以你可以更自由地选择最有意义的那个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.