当前位置:网站首页>6. Scala operator

6. Scala operator

2022-07-05 00:33:00 liangzai2048

Operator

Scala The use of operators and Java The use of operators is basically the same , Only a few details are different .


Arithmetic operator

  • Basic grammar
Operator operation Example result
+ Plus sign +33
- Minus sign b=4;-b4
+ Add 5+510
- reduce 6-42
* ride 3*412
/ except 5/51
% modulus ( Remainder )7%52
+ String addition “He”+“llo”“Hello”
  • For division marks "/" , Its integer and decimal division are different : Division between integers , Keep only the whole part and discard the decimal part .

  • Case study

package day03

object Test01_TestOperator {
    
  def main(args: Array[String]): Unit = {
    
    // 1、 Arithmetic operator 
    val result1 = 10 / 3
    println(result1)

    val result2: Double = 10 / 3
    println(result2)

    //float and double When doing decimal operations, there will be a deviation in accuracy 
    val result3: Double = 10.0 / 3
    println(result3)
    println(result3.formatted("%5.2f")) // There is not enough space in front of formatted output 

    val result4: Int = 10 % 3
    println(result4)
  }
}
 Running results :
3.0
3.3333333333333335
 3.33
1

Relational operator ( Comparison operator )

  • Basic grammar
Operator operation Example result
== Equivalent to 4==3false
!= It's not equal to 4!=3true
< Less than 4<3false
> Greater than 4>3true
<= Less than or equal to 4<=3false
>= Greater than or equal to 4>=3true
  • Case study

Java example

public class TestOperator {
    
    public static void main(String[] args) {
    
        //  Comparison operator 
        String s1 = "hello";
        String s2 = new String("hello");

        Boolean isEqual = s1 == s2;
        System.out.println(isEqual);
        System.out.println(s1.equals(s2));
    }
}
 Running results :
false
true

Scala example

package day03

object Test01_TestOperator {
    
  def main(args: Array[String]): Unit = {
    
    // 2、 Comparison operator 
    val s1: String = "hello"
    val s2: String = new String("hello")
    println(s1 == s2) // Judge whether the two values are equal 
    println(s1.equals(s2)) // Judge whether the two values are equal 
    println(s1.eq(s2)) // Determine if the addresses are equal 
  }
}
 Output results :
true
true
false

Logical operators

  • Basic grammar

Used to connect multiple conditions ( General classes will be relational expressions ), The end result is also a Boolean

Assume : Variable A by true,B by false

Operator describe example
&& Logic and (A && B ) The result of operation is false
II Logic or (A II B) The result of operation is true
! Logic is not !(A && B ) The result of operation is true
  • Case study
package day03

object Test01_TestOperator {
    
  def main(args: Array[String]): Unit = {
    
    // 3、 Logical operators 
    def m(n: Int): Int = {
    
      println("m Called ")
      return n
    }

    val n = 1
    println((4 > 5) && m(n) > 0) // A short circuit , The latter will not be called 
    println((4 > 5) & m(n) > 0)

    //  Determine whether a string is empty 
    def isNotRmpty(str: String): Boolean = {
    
      return str != null && !("".equals(str.trim))
    }

    println(isNotRmpty(str = null))
  }
}
 Running results :
false
m Called 
false
false

Assignment operator

  • Basic grammar

An assignment operator is the value of an operation , Assign to the specified variable

Operator describe example
= Simple assignment operators , Assign the value of an expression to an lvalue C= A+B take A+B The result of the expression is assigned to C
+= Add and then assign a value C+=A be equal to C=C+A
-= Subtract and then assign a value C-=A be equal to C=C-A
*= Multiply and assign a value C*=A be equal to C=C*A
/= Divide and assign a value C/=A be equal to C=C/A
%= The value is assigned after the remainder C%=A be equal to C=C%A
<<= Left shift assignment C<<=2 be equal to C=C<< 2
>>= Right shift after assignment C>>=2 be equal to C=C>>2
&= Bitwise and post assignment C&=2 be equal to C=C&2
^= Assign a value after bitwise XOR C ^ =2 be equal to C=C ^ 2
I= Assign value by bit or after CI=2 be equal to C=CI2

Be careful :Scala There is no ++、– The operator , Can pass +=、-= To achieve the effect of consent

  • Case study

Java

public class TestOperator {
    
    public static void main(String[] args) {
    
        //  Increase and decrease 
        int x = 15;
        int y = x++;
        System.out.println("x = " + x + ", y = " + y);

        x = 15;
        y = ++x;
        System.out.println("x = " + x + ", y = " + y);

        x = 23;
        x = x ++; // Temporary variable assignment required x++ temp = x++; x = temp;
        System.out.println(x); //  Assign first and then add 
    }
}
 Running results :
12
x = 16, y = 15
x = 16, y = 16
23

Scala

package day03

object Test01_TestOperator {
    
  def main(args: Array[String]): Unit = {
    
    // 4、 Assignment operator 
    var b: Byte = 10
// b += 1 // error
// println(b)

    var i: Int = 12
    i += 1
    println(i)
// i++ //Scala Abandoned 
  }
}
 Output results :
13

An operator

  • Basic grammar

Variables in the following table a by 60,b by 13

Operator describe example
& Bitwise and operator (a & b) Output results 12 , Binary interpretation : 0000 1100
I bitwise or operator (a I b) Output results 61 , Binary interpretation : 0011 1101
^ bitwise exclusive or operator (a ^ b) Output results 49 , Binary interpretation : 0011 0001
~ Bitwise negation operator (~a) Output results -61 , Binary interpretation : 1100 0011 , In the complement form of a signed binary number
<< Left move operator a<<2 Output results 240, Binary interpretation : 0011 0000
>> Move right operator a>> Output results 15, Binary interpretation : 0000 1111
  • Case study
package day03

object Test01_TestOperator {
    
  def main(args: Array[String]): Unit = {
    
    // 5、 An operator 
    val a = 60
    println(a << 3)
    println(a >> 2)

    val j: Short = -13
    println(j << 2)
    println(j >> 2)
    println(j >>> 2) // unsigned right shift 
  }
}
 Output results :
480
15
-52
-4
1073741820

The essence of operators

In the face of the object

package day03

object Test01_TestOperator {
    
  def main(args: Array[String]): Unit = {
    
    // 6、 The essence of operators 
    val j1: Int = 12
    val j2: Int = 37
    println(j1.+(j2))
    println(j1 + (j2))

    println(1.34.*(25))
    println(1.34 * 25)

    println(7.5 toInt).toString
  }
}
 Output results :
49
49
33.5
33.5
7

After all ! Give the pretty boy a compliment !(*゚ヮ゚)*

原网站

版权声明
本文为[liangzai2048]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202141119169610.html