# Section III of Scala

### Chapter Symbols

1. An overview of understanding operators
2. Master arithmetic, assignment, relationships, and the use of logical operators
3. Master swap variable cases
4. Understanding the use of bit operators

### 1.Arithmetic Operators

#### Introduction to 1.1 Operators

Symbols used to stitch variables or constants are called operators, while formulas connected by operators are called expressions.It is often used in actual development.

For example:

10 + 3 is an expression, and the + sign is an operator.

Note: Operators in Scala are not only operators, but also functions, which you can understand first and then we will explain in detail.

#### Classification of 1.2 Operators

• Arithmetic Operators

• Assignment Operators

• Relational Operators

• Logical operators

• Bitwise Operators

Note: There are no unary operators in Scala and they have been replaced by if-else.

#### 1.3 Arithmetic Operator

Arithmetic operators refer to symbols used to perform arithmetic operations. There are several common types:

operatorFunctional Interpretation
+Plus sign, function has 3 points.1) Represents positive 2) Ordinary addition 3) Stitching of strings
-Minus sign, function has 2 points.1) denotes negative number 2) ordinary subtraction operation
*Multiplication sign, used to get the product of two data
/Division, quotient used to get two data
%Remainder (also known as modulus), used to obtain the remainder of two data

Be careful:

1. There are no ++, --these two arithmetic operators in Scala, which is different from Java.

2. The result of integer division, or integer.If you want to get decimals, you must have floating-point data to participate.

For example: 10/3 result is 3 10/3.0 result is 3.3333 (infinite loop)

3. About + splicing strings: Any type of data and string splicing will result in a new string.

4. For the% operation, assuming a% B is evaluated, its underlying principle is: a - a/b * b

#### 1.4 Code Demo

Requirements: Demonstrates common operations of arithmetic operators.

Reference code:

```//Demo + Number Operation
println(+3)
println(10 + 3)
println("hello" + 10)

//Demo-Number Operation
println(-5)
println(10 - 5)

//Demo * Operation
println(5 * 3)

//Demo/Number Operation
println(10 / 3)
println(10 / 3.0)

//Demonstrate (Remaining) Operation
println(10 % 3)     //The result is 1, the specific operation: 10 - 10/3 * 3 = 10 - 3 * 3 = 1
println(10 % -3)    //The result is 1, the specific operation: 10 - 10/-3 *-3 = 10 - -3 *-3 = 10 - 9 = 1
println(-10 % 3)   //The result is -1, the specific operation process: -10 - -10/3 * 3 = -10 - -3 * 3 = -10 + 9 = -1
```

### 2.Assignment Operators

#### 2.1 Overview

Assignment operators refer to the symbols used for assignment operations.For example, assignment operators are used to assign a constant value, or a variable value, or even the result of execution of a piece of code to a variable.

#### 2.2 Classification

• There are two common types of assignment operators

• Basic assignment operator

=is the basic assignment operator, for example: var a:Int = 3, which assigns a constant value of 3 to a variable a

• Extended assignment operator

+=, -=, *=, /=, %=

Be careful:

1. The left side of an assignment operator must be: variable, not constant.For example: 3 = 5, this is wrong.

2. With regard to the extended assignment operator, you specify the operations for the data on the left and the data on the right, and assign the results to the left.

For example;a += 3 adds the value of variable a to constant 3 and assigns the result to variable a

#### 2.3 Code Demo

```//Assign constant value 1 to variable a
var a:Int = 1			//Note: Since subsequent code modifies the value of variable a, variable a is decorated with var
//Add 3 to variable a and reassign the result to variable a
a += 3			//The final result of a is: a = 4
//Subtract variable a by 2 and reassign the result to variable a
a -= 2			//The final result of a is: a = 2
//Multiply variable a by 3 and reassign the result to variable a
a *= 3			//The final result of a is: a = 6
//Divide variable a by 2 and reassign the result to variable a
a /= 2			//The final result of a is: a = 3
//Re-assign the result to variable a by taking the balance of variables A and 2
a %= 2			//The final result of a is: a = 1
```

### 3.Relational Operators

#### 3.1 Overview

Relational operators refer to symbols used for comparison operations.For example, whether the data is equal, unequal, big data 1 or big data 2, and so on.

#### 3.2 Classification

operatorFunctional Interpretation
>Used to determine if the front data is larger than the back data
>=Used to determine if the front data is greater than or equal to the back data
<Used to determine if the front data is smaller than the back data
<=Used to determine if the front data is less than or equal to the back data
==Used to determine if two data are equal
!=Used to determine if two data are not equal

Be careful:

1. Relational expressions, whether simple or complex, must end up with Boolean-type values, either true or false.
2. Never write==as=, otherwise the result may not be what you want.

#### 3.3 Code Demo

```//Defines two variables of type Int, A and b, with values of 3 and 5, respectively.
var a:Int = 3
var b:Int = 5
//Determine if a is greater than b and print the results
println(a > b)					//false
//Determine if a is greater than or equal to b and print the results
println(a >= 3)					//true
//Determine if a is less than b and print the results
println(a < b)					//true
//Determine if a is less than or equal to b and print the results
println(a <= 3)					//true
//Determine if a and b are not equal and print the results
println(a != b)					//true
//Determine if a and b are equal and print the results
println(a == b)					//false
//Writing== as= assigns the value of variable b to variable a
println(a = b)					//The output is a pair of parentheses'()', that is, no printed value.
println(a)						//Print variable a again and the result is: 5
```

#### 3.4 Relational Operator Extensions

My fellow Java learners found that the relational operator usage in Scala above is the same as in Java. Is there anything different from Java?

Requirement DescriptionScala CodeJava Code
Compare data values==or!=equals() method
Compare reference values (address values)eq method==or!=

Example

There is a string "abc" and a second string is created with the value: stitch an empty string after the first string.

Then use Compare to see if the two strings are equal and see if their reference values are equal.

Reference Code

```val s1 = "abc"
val s2 = s1 + ""
s1 == s2     //The result is: true, because the data values are compared
s1.eq(s2)	 //The result is: false, because the address values are compared
```

### 4.Logical operators

#### 4.1 Overview

Logical operators refer to symbols used for logical operations.It can be simply understood as: combinatorial judgment.For example, to determine whether multiple conditions are met or one of them is met, or even to reverse a certain result.

#### 4.2 Classification

operatorFunctional Interpretation
&&Logical AND, requires all conditions to be met (i.e., the result is true), simple memory: false is false as a whole.
||Logical or, requires that only one condition be met, simple memory: true as a whole.
!Logical NOT, used for negation.That is, before it was true, after it was false, before it was false, and after it was true.

Be careful:

1. Whether simple or complex, a logical expression must end up with a Boolean-type value, either true or false.
2. In Scala code, it is not possible to continuously invert a Boolean type of data, but it is possible in Java.
• That is:!!false, this will result in an error, which is not supported.

#### 4.3 Code Demo

```//Equivalent to: false & & true
println(3 > 5 && 2 < 3)		//The result is: false
//We can abbreviate the code as:
//Logical AND: With false, the whole is false.
println(false && true)		//The result is: false
println(true && false)		//The result is: false
println(false && false)		//The result is: false
println(true && true)		//The result is: true

println(false || true)		//The result is: true
println(true || false)		//The result is: true
println(false || false)		//The result is: false
println(true || true)		//The result is: true

println(!false)				//The result is: true
println(!true)				//The result is: false
println(!!true)				//This will cause errors. Scala does not support this, but Java code does.
```

### 5.Bitwise Operators

#### 5.1 paving knowledge

To learn bit operators well, you must know three things:

1. What is binary
2. What is 8421 yard
3. Rules for calculating the source, inverse and complement of integers

Generally speaking, every decimal is decimal, for example: every binary is binary, every decimal is decimal, there are several commonly used decimal:

Binary NameData Composition RulesExample
BinaryData starts with 0b (both upper and lower case) and consists of numbers 0 and 10b10001001, 0b00101010
Octal number systemData starts with 0 and consists of numbers 0~7064, 011
Decimal systemWrite the data directly, without a special start, consisting of numbers 0~910, 20, 333
HexadecimalThe data starts with 0x (both upper and lower case), consisting of numbers 0~9 and letters A-F (both upper and lower case)0x123F, 0x66ABC

Be careful:

With regard to binary data, the first one is called the sign bit, 0 is positive, 1 is negative.Other digits are: numeric digits.

For example: 0b10001001 result is a negative number, 0b00101010 result is a positive number.

8421 code is used to describe the relationship between binary and decimal data. It can help us quickly calculate the binary or decimal form of data.

The 8421 code corresponds to the following:

Binary bit 0 0 0 0 0 0 0 0 0 0

Corresponding decimal data 128 64 32 16 8 4 2 1

Be careful:

1. Rule of calculation: Binary bits from right to left, each bit more, corresponding decimal data multiplied by 2.

2. Tips for converting binary to decimal:

• Binary to decimal: Get the corresponding decimal data of the binary bit and add it up.
• For example: 0b101 corresponding decimal data calculation step: 4 + 0 + 1 = 5
• Decimal to binary: Decompose the decimal data to see which digits add up to equal it, then mark it as binary.
• For example: 10 corresponding binary data calculation steps: 10 = 8 + 2 = 0b1010
##### 5.1.3 Rules for computing the original complement of integers

The so-called original complement actually refers to binary data, which converts the decimal data into its corresponding binary data, which is the original code.

Note: The bottom level of computer storage, operation and operation of data are achieved by using the binary complement form of data.

• Positive number
• Positive numbers have the same source code, inverse code and complement code, so no special calculation is required.
• negative
• Inverse code calculation rules for negative numbers: the symbol bits of the original code are unchanged, and the number bits are reversed bitwise (formerly 0 now 1, formerly 1 now 0)
• Complement calculation rules for negative numbers: inverse code + 1

#### 5.2 Overview

Bit operators refer to the quick manipulation of data values by bit (Bit), which is only for integer data.Because the underlying computer stores, operates and operates in the form of binary complement of data, and we will often deal with a large amount of data in the future. To improve the efficiency of calculation, we can use bit operators to quickly modify data values.

#### 5.3 Classification

operatorFunctional Interpretation
&Bitwise And, Rule: 0 is 0, 1 is 1.
|Bitwise OR, rule: 1 1, 0 is 0.
^Bitwise XOR, rule: same 0, different 1.
~Bitwise reversal, rule: 0 changes 1, 1 changes 0.
<<Bitwise Left Shift, Rule: One bit per left, equal to the data multiplied by 2, for example: 2 << 1, result is 4
>>Bitwise Right Shift, Rule: One bit per right, equivalent to dividing the data by 2, for example: 6 >> 1, result is 3

Be careful:

1. Bit operators are only for integer data.
2. Operators operate on the binary complement form of data.
3. Tip: A number is bitwise different from or twice the same number, and the number value is unchanged.That is, 10 ^ 20 ^ 20, or 10

#### 5.4 Code Demo

```//Define two variables a and b with initialization values of 3 and 5, respectively
val a = 3			//Binary data: 0000 0011
val b = 5			//Binary data: 0000 0101

//The result is: 0000 0001, converted to decimal, the result is: 1
println(a & b)		//The printout is: 1

//The result is: 0000 0111, converted to decimal, result is: 7
println(a | b)		//The printout is: 7

//The result is: 0000 0110, converted to decimal, the result is: 6
println(a ^ b)		//The printout is: 6

//Calculation process: 1111 1100 (complement) -> 1111 1011 (inverse) -> 1000 0100 (source) -> decimal data: -4
println(~ a)		//The printout is: -4

//Calculation process: 10000011 (-3 source code) -> 1111 1100 (-3 inverse code) -> 1111 1101 (-3 complement) -> 0000 0010 (new complement after inversion) -> decimal data: 2
println(~ -3)		//The printout is: 2

//Calculation process: 0000 0011 (3's complement) -> 0000 1100 (new complement) -> decimal data: 12
println(a << 2)		//The printout is: 12

//Calculation process: 0000 0011 (3's complement) -> 0000 0001 (new complement) -> decimal data: 1
println(a >> 1)		//The printout is: 1

println(a ^ b ^ b)	//The printout is: 3
```

### 6.Case study: swapping values of two variables

#### 6.1 Requirements

Known to have two variables of type Int, A and b, with initialization values of 10 and 20, write code to exchange the values of variable a and B.

That is, the final result is: a=20, b=10.

Note: Direct writing of a=20, b=10 code is not allowed.

#### 6.2 Reference Code

• Mode one: through arithmetic operators.

```//Defines two variables of type Int, a and b, with initialization values of 10 and 20, respectively.
var a = 10
var b = 20
//Assign the results of the calculations for variables A and b to variable a
a = a + b	//a = 30, b = 20
//Calculate and assign values
b = a - b	//a = 30, b = 10
a = a - b	//a = 20, b = 10
//Print results
println("a: " + a)		//a: 20
println("b: " + b)		//b: 10
```
• Mode 2: by defining a temporary variable

```//Defines two variables of type Int, a and b, with initialization values of 10 and 20, respectively.
var a = 10
var b = 20
//Define the temp temporary variable, record the value of variable a
var temp = a			//a = 10, b = 20, temp = 10
//Assign the value of variable b to a
a = b					//a = 20, b = 20, temp = 10
//Assign the temp value of the temporary variable to b
b = temp				//a = 20, b = 10, temp = 10
//Print results
println("a: " + a)		//a: 20
println("b: " + b)		//b: 10
```
• Mode 3: through bitwise operators

```//Defines two variables of type Int, a and b, with initialization values of 10 and 20, respectively.
var a = 10
var b = 20
//Define the temp temporary variable, record the bitwise exclusive or value of variables a and b (this value does not need to be calculated)
var temp = a ^ b	//That is: temp = 10 ^ 20
//Swap variable values by bitwise exclusive or
a = a ^ temp		//Operation flow: a = a ^ temp = a ^ a ^ b = 10 ^ 10 ^ 20 = 20
b = b ^ temp		//Operation flow: b = b ^ temp = b ^ a ^ b = 20 ^ 10 ^ 20 = 10
//Print results
println("a: " + a)	//a: 20
println("b: " + b)	//b: 10
```

Tags: Java Scala Big Data

Posted on Mon, 11 Oct 2021 12:15:19 -0400 by bufhal