📜  Scala 中的运算符

📅  最后修改于: 2022-05-13 01:55:37.998000             🧑  作者: Mango

Scala 中的运算符

运算符是一个符号,表示要使用一个或多个操作数执行的操作。运算符是任何编程语言的基础。运算符允许我们对操作数执行不同类型的操作。 Scala 中使用了不同类型的运算符,如下所示:

算术运算符

这些用于对操作数执行算术/数学运算。

  • Addition(+)运算符将两个操作数相加。例如, x+y
  • 减法(-)运算符减去两个操作数。例如, xy
  • 乘法(*)运算符将两个操作数相乘。例如, x*y
  • Division(/)运算符将第一个操作数除以第二个操作数。例如, x/y
  • Modulus(%)运算符返回第一个操作数除以第二个操作数时的余数。例如, x%y
  • 指数(**)运算符返回操作数的指数(幂)。例如, x**y

例子:

Scala
// Scala program to demonstrate
// the Arithmetic Operators
 
object Arithop
{
 
def main(args: Array[String])
{
    // variables
    var a = 50;
    var b = 30;
     
    // Addition
    println("Addition of a + b = " + (a + b));
     
    // Subtraction
    println("Subtraction of a - b = " + (a - b));
     
    // Multiplication
    println("Multiplication of a * b = " + (a * b));
     
    // Division
    println("Division of a / b = " + (a / b));
     
    // Modulus
    println("Modulus of a % b = " + (a % b));
 
}
}


Scala
// Scala program to demonstrate
// the Relational Operators
object Relop
{
 
def main(args: Array[String])
{
    // variables
    var a = 50;
    var b = 30;
     
    // Equal to operator
    println("Equality of a == b is : " + (a == b));
     
    // Not equal to operator
    println("Not Equals of a != b is : " + (a != b));
     
    // Greater than operator
    println("Greater than of a > b is : " + (a > b));
     
    // Lesser than operator
    println("Lesser than of a < b is : " + (a < b));
 
    // Greater than equal to operator
    println("Greater than or Equal to of a >= b is : " + (a >= b));
     
    // Lesser than equal to operator
    println("Lesser than or Equal to of a <= b is : " + (a <= b));
 
}
}


Scala
// Scala program to demonstrate
// the Logical Operators
object Logop
{
 
def main(args: Array[String])
{
     
    // variables
    var a = false
    var b = true
     
    // logical NOT operator
    println("Logical Not of !(a && b) = " + !(a && b));
     
    // logical OR operator
    println("Logical Or of a || b = " + (a || b));
     
    // logical AND operator
    println("Logical And of a && b = " + (a && b));
 
}
}


Scala
// Scala program to demonstrate
// the Assignments Operators
object Assignop
{
 
def main(args: Array[String])
{
     
    // variables
    var a = 50;
    var b = 40;
    var c = 0;
     
    // simple addition
    c = a + b;
    println("simple addition: c= a + b = " + c);
     
    // Add AND assignment
    c += a;
    println("Add and assignment of c += a = " + c);
     
    // Subtract AND assignment
    c -= a;
    println("Subtract and assignment of c -= a = " + c);
     
    // Multiply AND assignment
    c *= a;
    println("Multiplication and assignment of c *= a = " + c);
     
    // Divide AND assignment
    c /= a;
    println("Division and assignment of c /= a = " + c);
     
    // Modulus AND assignment
    c %= a;
    println("Modulus and assignment of c %= a = " + c);
     
    // Left shift AND assignment
    c <<= 3;
    println("Left shift and assignment of c <<= 3 = " + c);
     
    // Right shift AND assignment
    c >>= 3;
    println("Right shift and assignment of c >>= 3 = " + c);
     
    // Bitwise AND assignment
    c &= a;
    println("Bitwise And assignment of c &= 3 = " + c);
     
    // Bitwise exclusive OR and assignment
    c ^= a;
    println("Bitwise Xor and assignment of c ^= a = " + c);
     
    // Bitwise inclusive OR and assignment
    c |= a;
    println("Bitwise Or and assignment of c |= a = " + c);
}
}


Scala
// Scala program to demonstrate
// the Bitwise Operators
object Bitop
{
def main(args: Array[String])
{
    // variables
    var a = 20;
    var b = 18;
    var c = 0;
     
    // Bitwise AND operator
    c = a & b;
    println("Bitwise And of a & b = " + c);
     
    // Bitwise OR operator
    c = a | b;
    println("Bitwise Or of a | b = " + c);
     
    // Bitwise XOR operator
    c = a ^ b;
    println("Bitwise Xor of a ^ b = " + c);
     
    // Bitwise once complement operator
    c = ~a;
    println("Bitwise Ones Complement of ~a = " + c);
     
    // Bitwise left shift operator
    c = a << 3;
    println("Bitwise Left Shift of a << 3 = " + c);
     
    // Bitwise right shift operator
    c = a >> 3;
    println("Bitwise Right Shift of a >> 3 = " + c);
     
    // Bitwise shift right zero fill operator
    c = a >>> 4;
    println("Bitwise Shift Right a >>> 4 = " + c);
}
}


输出:

Addition of a + b = 80
Subtraction of a - b = 20
Multiplication of a * b = 1500
Division of a / b = 1
Modulus of a % b = 20

关系运算符

关系运算符或运算符用于比较两个值。让我们一一看看:

  • Equal To(==)运算符检查两个给定操作数是否相等。如果是,则返回 true。否则返回false。例如, 5==5将返回 true。
  • Not Equal To(!=)运算符检查两个给定的操作数是否相等。如果不是,则返回 true。否则返回false。它是'=='运算符的精确布尔补码。例如, 5!=5将返回 false。
  • 大于(>)运算符检查第一个操作数是否大于第二个操作数。如果是,则返回 true。否则返回false。例如, 6>5将返回 true。
  • 小于(<)运算符检查第一个操作数是否小于第二个操作数。如果是,则返回 true。否则返回false。例如, 6<5将返回 false。
  • 大于等于 (>=)运算符检查第一个操作数是否大于或等于第二个操作数。如果是,则返回 true。否则返回false。例如, 5>=5将返回 true。
  • 小于等于 (<=)运算符检查第一个操作数是否小于或等于第二个操作数。如果是,则返回 true。否则返回false。例如, 5<=5也将返回 true。

例子:

斯卡拉

// Scala program to demonstrate
// the Relational Operators
object Relop
{
 
def main(args: Array[String])
{
    // variables
    var a = 50;
    var b = 30;
     
    // Equal to operator
    println("Equality of a == b is : " + (a == b));
     
    // Not equal to operator
    println("Not Equals of a != b is : " + (a != b));
     
    // Greater than operator
    println("Greater than of a > b is : " + (a > b));
     
    // Lesser than operator
    println("Lesser than of a < b is : " + (a < b));
 
    // Greater than equal to operator
    println("Greater than or Equal to of a >= b is : " + (a >= b));
     
    // Lesser than equal to operator
    println("Lesser than or Equal to of a <= b is : " + (a <= b));
 
}
}

输出:

Equality of   a == b is : false
Not Equals of a != b is : true
Greater than of a > b is : true
Lesser than of  a = b is : true
Lesser than or Equal to of a <= b is : false

逻辑运算符

它们用于组合两个或多个条件/约束或补充考虑的原始条件的评估。它们描述如下:

  • 当两个条件都满足时,逻辑 AND(&&)运算符返回 true。否则返回false。使用“”是 &&运算符的替代方法。例如,当 a 和 b 都为真(即非零)时, a && b返回真。
  • 当考虑的一个(或两个)条件满足时,逻辑 OR(||)运算符返回 true。否则返回false。使用“”是 || 的替代方法。运算符。例如,一个 ||如果 a 或 b 之一为真(即非零),则b返回真。当然,当 a 和 b 都为真时,它返回真。
  • 逻辑 NOT(!)运算符在考虑的条件不满足时返回 true。否则返回false。使用“ not ”是 !运算符。例如, !true返回 false。

例子:

斯卡拉

// Scala program to demonstrate
// the Logical Operators
object Logop
{
 
def main(args: Array[String])
{
     
    // variables
    var a = false
    var b = true
     
    // logical NOT operator
    println("Logical Not of !(a && b) = " + !(a && b));
     
    // logical OR operator
    println("Logical Or of a || b = " + (a || b));
     
    // logical AND operator
    println("Logical And of a && b = " + (a && b));
 
}
}

输出:

Logical Not of !(a && b) = true
Logical Or of a || b = true
Logical And of a && b = false

赋值运算符

赋值运算符用于为变量赋值。赋值运算符的左侧操作数是一个变量,而赋值运算符符的右侧操作数是一个值。右侧的值必须与左侧变量的数据类型相同,否则编译器将引发错误。
不同类型的赋值运算符如下所示:

  • 简单赋值 (=)运算符是最简单的赋值运算符。该运算符用于将右侧的值赋给左侧的变量。
  • Add AND Assignment (+=)运算符用于将左操作数与右操作数相加,然后将其分配给左侧的变量。
  • 减与赋值 (-=)运算符用于将左操作数与右操作数相减,然后将其分配给左侧的变量。
  • 乘与赋值 (*=)运算符用于将左操作数与右操作数相乘,然后将其分配给左侧的变量。
  • 除法和赋值(/=)运算符用于将左操作数与右操作数相除,然后将其分配给左侧的变量。
  • 模数与赋值 (%=)运算符用于将左操作数与右操作数进行模数赋值,然后将其赋值给左边的变量。
  • 指数与赋值 (**=)运算符用于将左操作数的幂提高到右操作数并将其分配给左侧的变量。
  • 左移 AND 赋值(<<=)运算符用于将左操作数与右操作数进行二进制左移并将其分配给左侧的变量。
  • 右移 AND 赋值(>>=)运算符用于将左操作数与右操作数进行二进制右移并将其分配给左侧的变量。
  • 位与赋值(&=)运算符用于将左操作数与右操作数进行位与,并将其分配给左侧的变量。
  • 按位异或和赋值(^=)运算符用于将左操作数与右操作数进行按位异或,并将其分配给左侧的变量。
  • 按位或和赋值(|=)运算符用于将左操作数与右操作数进行按位或,并将其分配给左侧的变量。

例子:

斯卡拉

// Scala program to demonstrate
// the Assignments Operators
object Assignop
{
 
def main(args: Array[String])
{
     
    // variables
    var a = 50;
    var b = 40;
    var c = 0;
     
    // simple addition
    c = a + b;
    println("simple addition: c= a + b = " + c);
     
    // Add AND assignment
    c += a;
    println("Add and assignment of c += a = " + c);
     
    // Subtract AND assignment
    c -= a;
    println("Subtract and assignment of c -= a = " + c);
     
    // Multiply AND assignment
    c *= a;
    println("Multiplication and assignment of c *= a = " + c);
     
    // Divide AND assignment
    c /= a;
    println("Division and assignment of c /= a = " + c);
     
    // Modulus AND assignment
    c %= a;
    println("Modulus and assignment of c %= a = " + c);
     
    // Left shift AND assignment
    c <<= 3;
    println("Left shift and assignment of c <<= 3 = " + c);
     
    // Right shift AND assignment
    c >>= 3;
    println("Right shift and assignment of c >>= 3 = " + c);
     
    // Bitwise AND assignment
    c &= a;
    println("Bitwise And assignment of c &= 3 = " + c);
     
    // Bitwise exclusive OR and assignment
    c ^= a;
    println("Bitwise Xor and assignment of c ^= a = " + c);
     
    // Bitwise inclusive OR and assignment
    c |= a;
    println("Bitwise Or and assignment of c |= a = " + c);
}
}

输出:

simple addition: c= a + b = 90
Add and assignment of c += a = 140
Subtract and assignment of c -= a = 90
Multiplication and assignment of c *= a = 4500
Division and assignment of c /= a = 90
Modulus and assignment of c %= a = 40
Left shift and assignment of c <<= 3 = 320
Right shift and assignment of c >>= 3 = 40
Bitwise And assignment of c &= 3 = 32
Bitwise Xor and assignment of c ^= a = 18
Bitwise Or and assignment of c |= a = 50

位运算符

在 Scala 中,有 7 个位运算运算符,它们在位级别工作或用于执行逐位操作。以下是按位运算运算符:

  • 按位与 (&):将两个数字作为操作数,并对两个数字的每一位进行 AND。仅当两个位都为 1 时,AND 的结果才为 1。
  • 按位或 (|):将两个数字作为操作数,对两个数字的每一位进行 OR。 OR 的结果为 1 两位中的任何一位为 1。
  • 按位异或 (^):将两个数作为操作数,对两个数的每一位进行异或。如果两个位不同,则异或的结果为 1。
  • 按位左移(<<):取两个数,左移第一个操作数的位,第二个操作数决定移位的位数。
  • 按位右移(>>):取两个数,右移第一个操作数的位,第二个操作数决定移位的位数。
  • 按位补码 (~):该运算符采用单个数字,用于执行 8 位的补码运算。
  • 按位右移零填充(>>>):在右移零填充运算符中,左操作数右移右操作数指定的位数,移位后的值用零填充。

例子:

斯卡拉

// Scala program to demonstrate
// the Bitwise Operators
object Bitop
{
def main(args: Array[String])
{
    // variables
    var a = 20;
    var b = 18;
    var c = 0;
     
    // Bitwise AND operator
    c = a & b;
    println("Bitwise And of a & b = " + c);
     
    // Bitwise OR operator
    c = a | b;
    println("Bitwise Or of a | b = " + c);
     
    // Bitwise XOR operator
    c = a ^ b;
    println("Bitwise Xor of a ^ b = " + c);
     
    // Bitwise once complement operator
    c = ~a;
    println("Bitwise Ones Complement of ~a = " + c);
     
    // Bitwise left shift operator
    c = a << 3;
    println("Bitwise Left Shift of a << 3 = " + c);
     
    // Bitwise right shift operator
    c = a >> 3;
    println("Bitwise Right Shift of a >> 3 = " + c);
     
    // Bitwise shift right zero fill operator
    c = a >>> 4;
    println("Bitwise Shift Right a >>> 4 = " + c);
}
}

输出:

Bitwise And of a & b = 16
Bitwise Or of a | b = 22
Bitwise Xor of a ^ b = 6
Bitwise Ones Complement of ~a = -21
Bitwise Left Shift of a << 3 = 160
Bitwise Right Shift of a >> 3 = 2
Bitwise Shift Right a >>> 4 = 1