本文共 9032 字,大约阅读时间需要 30 分钟。
In this post, we are going to discuss about some basic concepts of Scala Programming Language. Even though, it covers very basic concepts but they are very important to know.
在本文中,我们将讨论Scala编程语言的一些基本概念。 即使它涵盖了非常基本的概念,但了解它们也非常重要。
We can answer some questions like What is val, var and def? What are the differences between them? When they are evaluated? What are the differences between val and lazy val?. These things are required to start working on Scala-Based Projects
我们可以回答一些问题,例如什么是val,var和def? 它们之间有什么区别? 什么时候进行评估? val和lazy val有什么区别? 开始基于Scala的项目时需要这些东西
In Scala, ‘val’ modifier is used to define constants. val means constant or immutable that means we cannot change it’s value once its created.
在Scala中,“ val”修饰符用于定义常量。 val表示常量或不可变,表示一旦创建它便无法更改其值。
Example:-
例:-
scala> val name:String = "Scala"name: String = Scalascala> nameres2: String = Scalascala> name = "Java":11: error: reassignment to val name = "Java" ^
Here, we can observe that reassignment to val is prohibited. That means val is used to define Immutable data.
在这里,我们可以看到禁止重新分配给val。 这意味着val用于定义不可变数据。
In Scala, ‘val’ is used to define constants. It evaluates only once. They are evaluated at the time of definition only. Once its evaluated, it reuses same value for all references of it.
在Scala中,“ val”用于定义常量。 它只评估一次。 仅在定义时评估它们。 一旦对其求值,它将为所有引用重复使用相同的值。
It does NOT evaluate every-time we access it.
它不会在我们每次访问它时进行评估。
We will explore this rule by using one simple example as shown below:
我们将通过一个简单的示例来探索此规则,如下所示:
Example:-
例:-
object ValApp extends App{ val number = { println("Constant number is initialized."); 99 } println("Before Accessing 'number' constant:") println(number + 1) println(number + 1) println(number + 1)}
Output:-
Here I’m using all REPL commands for simplicity purpose only. We can test them using any Scala IDEs like Eclipse Scala IDE or IntelliJ IDE (My favourite IDE).输出:-
在这里,我仅出于简化目的使用所有REPL命令。 我们可以使用任何Scala IDE(例如Eclipse Scala IDE或IntelliJ IDE(我最喜欢的IDE))对其进行测试。F:\>scalac ValApp.scalaF:\>scala ValAppConstant number is initialized.Before Accessing 'number' constant:100100100
Here we can observe that “number” Constant is evaluated only once at the time of definition that’s why println output is printed before accessing the variable. Even though we have accessed number Constant three times, it’s initialized only once and printed “Constant number is initialized.” text only once.
在这里,我们可以观察到“数字”常量在定义时仅被评估一次,这就是为什么在访问变量之前先打印println输出的原因。 即使我们已经访问过3次常数,它也只能初始化一次并显示“常数已初始化”。 仅发送一次文字。
In Scala, ‘var’ modifier is used to define variables. var means Variable or Mutable that means we can change it’s value once its created.
在Scala中,“ var”修饰符用于定义变量。 var表示Variable或Mutable,表示一旦创建它就可以更改其值。
Example:-
例:-
scala> var name:String = "Scala"name: String = Scalascala> nameres0: String = Scalascala> name = "Java"name: String = Javascala> nameres1: String = Java
Here, we can observe that reassignment to var is allowed. As var is used to define Mutable data, we can change its value once its created.
在这里,我们可以看到允许重新分配给var。 由于var用于定义Mutable数据,因此我们可以在创建变量后更改其值。
var is evaluated at the time of definition
It is evaluated only once在定义时评估var
仅评估一次Example:-
例:-
object VarApp extends App{ val number = { println("Variable number is initialized."); 99 } println("Before Accessing 'number' variable:") println(number + 1) println(number + 1) println(number + 1)}
Output:-
输出:-
F:\>scalac VarApp.scalaF:\>scala VarAppVariable number is initialized.Before Accessing 'number' variable:100100100
In Scala, def is used to define functions or methods. A Method or Function may or may not have arguments and may or may not have return type.
在Scala中,def用于定义函数或方法。 方法或函数可能具有或不具有参数,并且可能具有或不具有返回类型。
Example:-
例:-
scala> def add(num1:Int, num2:Int): Int = num1 + num2add: (num1: Int, num2: Int)Intscala> add(11,22)res2: Int = 33
As shown above, Method or Function is evaluated only we make a call to it.
如上所示,仅在调用方法或函数时才对其进行评估。
In Scala, def is evaluated lazily. It is not evaluated at the time of definition. It is evaluated whenever we make a call to it. It is evaluated every-time we make a call to it.
在Scala中,def的计算是延迟的。 在定义时不进行评估。 每当我们调用它时,都会对其进行评估。 每次调用它时都会对其进行评估。
Example:-
例:-
object DefApp extends App{ def tax = { println("Function execution started.") 1100 } println(tax) println(tax) println(tax)}
Output:-
输出:-
Before making a cll to Function:Function execution started.1100Function execution started.1100Function execution started.1100
As shown above output, method or function is evaluated only when we make a call to it. Otherwise, it is not evaluated.
如上面的输出所示,仅在调用方法或函数时才对其进行评估。 否则,将不进行评估。
As we know, val is used to define constants or Immutable Data. In the same way, we use “lazy val” to define Immutable data.
众所周知,val用于定义常量或不可变数据。 同样,我们使用“惰性值”定义不可变数据。
However, lazy val is evaluated lazily and only once. It is evaluated only once when we use it for first time.
It is not evaluated at the time of definition. It is not evaluated every-time we access it.但是,懒惰val只能被懒惰地评估一次。 当我们第一次使用它时,它只会被评估一次。
在定义时不进行评估。 我们不会在每次访问它时对其进行评估。Example:-
例:-
object LazyValApp extends App{ lazy val number = { println("Constant number is initialized."); 99 } println("Before Accessing 'number' constant:") println(number + 1) println(number + 1) println(number + 1) }
Output:-
输出:-
Before Accessing 'number' constant:Constant number is initialized.100100100
Here I’m using same example of ValApp, but changed from “val” to “lazy val” that’s it.
在这里,我使用的是ValApp的相同示例,但仅从“ val”更改为“ lazy val”。
If we observe this output, we can see that we are not seeing “Constant number is initialized.” output at the time of definition. We are seeing this message only at the time of first access. It’s displaying that message only once right. Cool!.
如果观察到此输出,则可以看到没有看到“常量号已初始化”。 定义时的输出。 我们仅在首次访问时才看到此消息。 它仅一次显示该消息。 凉!。
NOTE:- We cannot use “lazy” modifier for var. It’s allowed to use only for val.
Example:-注意:-我们不能对var使用“惰性”修饰符。 只允许用于val。
例:-scala> lazy var a = 0:1: error: lazy not allowed here. Only vals can be lazylazy var a = 0 ^
Here we will discuss some similarities and differences between val and lazy val constructs in Scala Language.
在这里,我们将讨论Scala语言中val和惰性val构造之间的一些异同。
Similarities between val and lazy val constructs:-
val和惰性val构造之间的相似之处:
Differences between val and lazy val constructs:-
val和惰性val构造之间的区别:
In previous sections, we have discussed about Scala val,var,lazy val and def Constructs in-detail with some simple and useful examples. Here are some bullet points to remember.
在前面的部分中,我们通过一些简单而有用的示例详细讨论了Scala val,var,lazy val和def构造。 这里有一些要记住的要点。
NOTE:-In Scala, “by-name” arguments are evaluated/computed every time we access them. As it’s very big concept, we will discuss about it in a separate post soon.
注意:-在Scala中,每次访问它们时,都会对“按名称”参数进行评估/计算。 由于它是一个非常大的概念,因此我们很快将在另一篇文章中进行讨论。
That’s it all about Scala val,var, def and lazy val constructs usage and evaluation. We will discuss some more Scala concepts in my coming posts.
这就是关于Scala val,var,def和lazy val构造用法和评估的全部内容。 我们将在我的后续文章中讨论更多Scala概念。
Please drop me a comment if you like my post or have any typo errors/issues/suggestions.
如果您喜欢我的帖子或有任何错字/问题/建议,请给我评论。
翻译自:
转载地址:http://fdqzd.baihongyu.com/