Scala运算符–算术,关系,逻辑,按位,赋值

时间:2020-02-23 14:41:47  来源:igfitidea点击:

今天,我们将研究各种类型的scala运算符。
运算符告诉编译器执行特定的数学或者逻辑运算。

Scala运算符

Scala编程语言支持以下类型的运算符。

  • 算术运算符
  • 关系运算符
  • 逻辑运算符
  • 按位运算符
  • 赋值运算符

让我们逐个示例地进行介绍。

Scala算术运算符

算术运算符包括加法,减法,乘法,除法和模数之类的基本运算。
支持的算术运算符为+,-,*和/。

OperatorDescription
+Adds two operands
Subtracts the second operand from the first
*Multiplies two operands
/Divides the numerator by the denominator
%Returns the remainder

这是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));

}
}

我们正在使用main方法定义一个对象算术,该方法接受数组的String类型的参数。
x和y变量分别声明为值40和20。
我们通过执行x + y,x-y,x * y,x/y,x%y来打印结果。

请注意,在Scala中,与Java不同,我们可以直接创建对象而无需先创建类。

在Scala shell中键入Arithmetic.main(null)创建对象算术后,运行上述代码。

输出:

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

Scala关系运算符

关系运算符包括==,!=,>,<,> =和<=。
下表显示了scala中的关系运算符。

OperatorDescription
==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.

考虑一个用于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));

}
}

我们使用main方法定义一个对象Relational,该方法接受数组的String类型的参数。
x和y变量分别声明为值10和20。
我们通过执行x == y,x!= y,x> y,x <y,x> = y和x <= y的操作来打印结果。

通过键入Relational.main(null)运行以上代码

输出:

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

Scala逻辑运算符

逻辑运算符包括!,||和&&。
下表列出了支持的逻辑运算符。

OperatorDescription
!Logical NOT operand which reverses the logical state of the operand.Returns true if the condition is satisfied
&&Logical AND operand which returns true if both the operand are non zero

考虑一个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) );

}
}

我们正在使用main方法定义一个逻辑对象,该方法接受数组的String类型的参数。
x和y是布尔变量,其值分别为false和true。
我们通过执行!(x && y),x ||打印结果。
y和x && y。

通过键入Logical.main(null)运行以上代码

输出:

scala> Logical.main(null)
Logical Not of !(x && y) = true
Logical Or of x || y = true
Logical And of x && y = false

Scala中的按位运算符

按位运算符包括&,|,~,^,<<,>>和>>>。
下表显示了Scala支持的按位运算符。

OperatorDescription
&Binary AND operator copies the bit to the result if the operator exists in both 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.

考虑一个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 );
}
}

我们使用main方法定义一个按位对象,该方法接受数组的String类型的参数。
x和y是分别具有值16和12的变量。
我们通过执行所有按位运算来打印结果。

通过键入Bitwise.main(null)运行程序

输出:

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赋值运算符

Scala赋值运算符包括=,+ =,-=,* =,/=,%=,<< =,>> =,&=,| =和^ =。
下表显示了赋值运算符的列表。

OperatorDescription
=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
=
^=Performs bitwise exclusive OR operation and assigns result to the left operand

考虑一个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 );
}
}

我们正在使用main方法定义一个对象Assignment,该方法接受array的String类型的参数。
x和y是变量。
我们正在通过执行所有分配操作来打印结果。

通过绑定Assignment.main(null)运行以上示例

输出:

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