scala运算符
Today we will look into various types of scala operator. An operator tells the compiler to perform specific mathematical or logical operations.
今天,我们将研究各种类型的scala运算符。 操作员告诉编译器执行特定的数学或逻辑运算。
The following types of operators are supported in Scala programming language.
Scala编程语言支持以下类型的运算符。
Let’s go through with each of them one by one with examples.
让我们逐个示例地进行研究。
Arithmetic operators include the basic operations like Addition, Subtraction, Multiplication, Division and Modulus. The arithmetic operators supported are +,-,*,/ and %.
算术运算符包括基本运算,例如加法,减法,乘法,除法和模数。 支持的算术运算符为+,-,*和/。
Operator | Description |
+ | Adds two operands |
– | Subtracts the second operand from the first |
* | Multiplies two operands |
/ | Divides the numerator by the denominator |
% | Returns the remainder |
操作员 | 描述 |
+ | 加两个操作数 |
– | 从第一个减去第二个操作数 |
* | 将两个操作数相乘 |
/ | 分子除以分母 |
% | 返回余数 |
Here is an example of scala arithmetic operator.
这是scala算术运算符的示例。
object Arithmetic {
def main(args:Array[String]) {
var x = 40;
var y = 20;
println("Addition of x + y = " + (x + y));
println("Subtraction of x - y = " + (x - y));
println("Multiplication of x * y = " + (x * y));
println("Division of x / y = " + (x / y));
println("Modulus of x % y = " + (x % y));
}
}
We are defining an object Arithmetic with the main method which accepts argument of String type of array. x and y variables are declared with values 40 and 20 respectively. We are printing the result by performing operations x+y, x-y, x*y, x/y, x%y.
我们正在使用main方法定义一个对象算术,该方法接受array的String类型的参数。 x和y变量分别声明为值40和20。 我们通过执行x + y,xy,x * y,x / y,x%y操作来打印结果。
Note that in Scala unlike in Java we can directly create an object without first creating a class.
请注意,在Scala中,与Java不同,我们可以直接创建对象而无需先创建类。
Run the above code after creating object Arithmetic by typing Arithmetic.main(null)
in Scala shell.
通过在Scala shell中键入Arithmetic.main(null)
创建对象Arithmetic后,运行上述代码。
Output:
输出:
scala> Arithmetic.main(null)
Addition of x + y = 60
Subtraction of x - y = 20
Multiplication of x * y = 800
Division of x / y = 2
Modulus of x % y = 0
Relational operators include ==,!=,>,<,>= and <=. The following table shows the list of relational operators in scala.
关系运算符包括==,!=,>,<,> =和<=。 下表显示了scala中的关系运算符。
Operator | Description |
== | Checks whether the two operands are equal or not and returns true if they are equal. |
!= | Checks if the two operands are equal or not and returns true if they are not equal. |
> | Checks if the first operand is greater than the second and returns true if the first operand is greater than the second operand. |
< | Checks if the first operand is lesser than the second and returns true if the first operand is lesser than the second operand. |
>= | Checks whether the first operand is greater than or equal to the second operand and returns true if the first operand is greater than or equal to the second operand. |
<= | Checks whether the first operand is lesser than or equal to the second operand and returns true if the first operand is lesser than or equal to the second operand. |
操作员 | 描述 |
== | 检查两个操作数是否相等,如果相等,则返回true。 |
!= | 检查两个操作数是否相等,如果不相等则返回true。 |
> | 检查第一个操作数是否大于第二个操作数,如果第一个操作数大于第二个操作数,则返回true。 |
< | 检查第一个操作数是否小于第二个操作数,如果第一个操作数小于第二个操作数,则返回true。 |
> = | 检查第一个操作数是否大于或等于第二个操作数,如果第一个操作数大于或等于第二个操作数,则返回true。 |
<= | 检查第一个操作数是否小于或等于第二个操作数,如果第一个操作数小于或等于第二个操作数,则返回true。 |
Consider an example for relational operators in scala.
考虑一个用于scala中关系运算符的示例。
object Relational {
def main(args:Array[String]) {
var x = 10;
var y = 20;
println("Equality of x == y is : " + (x == y));
println("Not Equals of x != y is : " + (x !=y));
println("Greater than of x > y is : " + (x > y));
println("Lesser than of x < y is : " + (x < y));
println("Greater than or Equal to of x >= y is : " + (x >= y));
println("Lesser than or Equal to of x <= y is : " + (x <= y));
}
}
We are defining an object Relational with the main method which accepts argument of String type of array. x and y variables are declared with values 10 and 20 respectively. We are printing the result by performing operations x ==y, x!=y, x>y, x
我们正在使用main方法定义一个对象Relational,该方法接受数组的String类型的参数。 x和y变量分别声明为值10和20。 我们通过执行x == y,x!= y,x> y,x
Run the above code by typing Relational.main(null)
通过键入Relational.main(null)运行以上代码
Output:
输出:
scala> Relational.main(null)
Equality of x == y is : false
Not Equals of x != y is : true
Greater than of x > y is : false
Lesser than of x < y is : true
Greater than or Equal to of x >= y is : false
Lesser than or Equal to of x <= y is : true
Logical operators include !, || and &&. The following table shows the list of logical operators supported.
逻辑运算符包括!,|| 和&&。 下表列出了支持的逻辑运算符。
Operator | Description |
! | Logical NOT operand which reverses the logical state of the operand.Returns true if the condition is satisfied |
|| | Logical OR operand which returns true if any of the two operands is non zero |
&& | Logical AND operand which returns true if both the operand are non zero |
操作员 | 描述 |
! | 逻辑非操作数,它会反转操作数的逻辑状态。如果满足条件,则返回true |
|| | 逻辑OR操作数,如果两个操作数中的任何一个都不为零,则返回true |
&& | 逻辑AND操作数,如果两个操作数都不为零,则返回true |
Consider an example of scala logical operators.
考虑一个scala逻辑运算符的示例。
object Logical {
def main(args: Array[String]) {
var x = false
var y = true
println("Logical Not of !(x && y) = " + !(x && y) );
println("Logical Or of x || y = " + (x || y) );
println("Logical And of x && y = " + (x &&y) );
}
}
We are defining an object Logical with the main method which accepts argument of String type of array. x and y are boolean variables with values false and true respectively. We are printing the result by performing operations !(x && y), x || y and x && y.
我们正在使用main方法定义一个逻辑对象,该方法接受数组的String类型的参数。 x和y是布尔变量,其值分别为false和true。 我们通过执行!(x && y),x ||打印结果。 y和x && y。
Run the above code by typing Logical.main(null)
通过键入Logical.main(null)运行以上代码
Output:
输出:
scala> Logical.main(null)
Logical Not of !(x && y) = true
Logical Or of x || y = true
Logical And of x && y = false
Bitwise operators include &, |, ~, ^, <<, >> and >>>. The following table shows the bitwise operators supported in Scala.
按位运算符包括&,|,〜,^,<<,>>和>>>。 下表显示了Scala支持的按位运算符。
Operator | Description |
& | Binary AND operator copies the bit to the result if the operator exists in both the operands. |
| | Binary OR operator copies the bit to the result if the operator exists in either of the operands. |
~ | Binary Ones Complement Operator is unary and has effects of the flipping bits. |
^ | Binary XOR operator copies if the bit is set in one of the operand but not both. |
<< | Binary Left Shift operator.The left operand value is moved left by the number of bits specified in the right operand. |
>> | Binary Right Shift operator.The left operand value is moved right by the number of bits specified by the right operand. |
>>> | Shift right zero fill operator. The left operands value is moved right by the number of bits specified by the right operand and shifted values are filled with zeros. |
操作员 | 描述 |
和 | 如果两个操作数中都存在该运算符,则二进制AND运算符会将位复制到结果中。 |
| | 如果该运算符存在于两个操作数中,则二进制OR运算符会将其复制到结果中。 |
〜 | 二进制补码运算符是一元的,并且具有翻转位的作用。 |
^ | 如果将位设置为操作数之一而不是两个都设置,则二进制XOR运算符将复制。 |
<< | Binary Left Shift运算符。左操作数的值向左移动右操作数中指定的位数。 |
>> | 二进制右移运算符。左操作数的值向右移动右操作数指定的位数。 |
>>> | 右移零填充运算符。 左操作数的值向右移动右操作数指定的位数,移位后的值用零填充。 |
Consider an example of bitwise operator in scala.
考虑一下scala中按位运算符的示例。
object Bitwise {
def main(args: Array[String]) {
var x = 16;
var y = 12;
var z = 0;
z = x & y;
println("Bitwise And of x & y = " + z );
z = x | y;
println("Bitwise Or of x | y = " + z );
z = x ^ y;
println("Bitwise Xor of x ^ y = " + z );
z = ~x;
println("Bitwise Ones Complement of ~x = " + z );
z = x << 2;
println("Bitwise Left Shift of x << 2 = " + z );
z = x >> 2;
println("Bitwise Right Shift of x >> 2 = " + z );
z = x >>> 2;
println("Bitwise Shift Right x >>> 2 = " + z );
}
}
We are defining an object Bitwise with the main method which accepts argument of String type of array. x and y are variables with values 16 and 12 respectively. We are printing the result by performing all bitwise operations.
我们使用main方法定义一个按位对象,该方法接受数组的String类型的参数。 x和y是分别具有值16和12的变量。 我们通过执行所有按位运算来打印结果。
Run the program by typing Bitwise.main(null)
通过键入Bitwise.main(null)运行程序
Output:
输出:
scala> Bitwise.main(null)
Bitwise And of x & y = 0
Bitwise Or of x | y = 28
Bitwise Xor of x ^ y = 28
Bitwise Ones Complement of ~x = -17
Bitwise Left Shift of x << 2 = 64
Bitwise Right Shift of x >> 2 = 4
Bitwise Shift Right x >>> 2 = 4
Scala Assignment operators include =,+=, -=, *=, /=, %=, <<=, >>=, &=, |= and ^=.The following table shows the list of assignment operators.
Scala赋值运算符包括=,+ =,-=,* =,/ =,%=,<< =,>> =,&=,| =和^ =。下表显示了赋值运算符的列表。
Operator | Description |
= | Assigns value from right side operand to left side operand |
+= | Adds right operand to left operand and assigns the result to left operand |
-= | Subtracts right operand from the left operand and assigns the result to left operand |
*= | Multiplies right operand with the left operand and assigns the result to the left operand |
/= | Divides the left operand with the right operand and assigns the result to the left operand |
%= | Finds the modulus of two operands and assigns the result to left operand |
<<= | Left shift assignment operator.It left shifts the operand and assigns result to the left operand |
>>= | Right shift assignment operator.It rights shifts the operator and assigns the value to left operand |
&= | Bitwise AND assignment operator performs bitwise AND operation and assigns the result to left operand |
|= | Bitwise OR assignment operator performs bitwise OR operation and assigns result to left operand |
^= | Performs bitwise exclusive OR operation and assigns result to the left operand |
操作员 | 描述 |
= | 将值从右侧操作数分配给左侧操作数 |
+ = | 向左操作数添加右操作数,并将结果分配给左操作数 |
-= | 从左操作数中减去右操作数,并将结果分配给左操作数 |
* = | 将右操作数与左操作数相乘,并将结果分配给左操作数 |
/ = | 将左操作数除以右操作数,并将结果分配给左操作数 |
%= | 查找两个操作数的模数并将结果分配给左操作数 |
<< = | 左移赋值运算符,它向左移操作数并将结果赋给左操作数 |
>> = | 右移赋值运算符,它会右移运算符并将值赋给左操作数 |
&= | 按位与赋值运算符执行按位与运算并将结果赋给左操作数 |
| = | 按位或赋值运算符执行按位或运算并将结果赋给左操作数 |
^ = | 执行按位异或运算并将结果分配给左操作数 |
Consider an example for assignment operators in scala.
考虑一个Scala中赋值运算符的示例。
object Assignment {
def main(args: Array[String]) {
var x = 20;
var y = 30;
var z = 0;
z = x + y;
println("z= x+ y = " + z );
z+= x ;
println("Add and assignment of z += x = " + z );
z -= x ;
println("Subtract and assignment of z -= x = " + z );
z *= x ;
println("Multiplication and assignment of z *= x = " + z );
x = 20;
z = 15;
z /= x ;
println("Division and assignment of z /= x = " + z );
x = 30;
z = 15;
z %= x;
println("Modulus and assignment of z %= x = " + z );
z <<= 2;
println("Left shift and assignment of z <<= 2 = " + z );
z >>= 2;
println("Right shift and assignment of z >>= 2 = " + z );
z &= x;
println("Bitwise And assignment of z &= 2 = " +z );
z ^= x ;
println("Bitwise Xor and assignment of z ^= x = " + z );
z |= x ;
println("Bitwise Or and assignment of z |= x = " + z );
}
}
We are defining an object Assignment with the main method which accepts argument of String type of array. x and y are variables. We are printing the result by performing all assignment operations.
我们正在使用main方法定义一个对象Assignment,该方法接受array的String类型的参数。 x和y是变量。 我们正在通过执行所有分配操作来打印结果。
Run the above example by tying Assignment.main(null)
通过绑定Assignment.main(null)运行以上示例
Output:
输出 :
scala> Assignment.main(null)
z= x+ y = 50
Add and assignment of z += x = 70
Subtract and assignment of z -= x = 50
Multiplication and assignment of z *= x = 1000
Division and assignment of z /= x = 0
Modulus and assignment of z %= x = 15
Left shift and assignment of z <<= 2 = 60
Right shift and assignment of z >>= 2 = 15
Bitwise And assignment of z &= 2 = 14
Bitwise Xor and assignment of z ^= x = 16
Bitwise Or and assignment of z |= x = 30
这就是不同类型的Scala运算符的全部。
翻译自: https://www.journaldev.com/7589/scala-operator-arithmetic-relational-logical-bitwise-assignment
scala运算符