当前位置:网站首页>6. Scala operator
6. Scala operator
2022-07-05 00:33:00 【liangzai2048】
List of articles
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 | +3 | 3 |
- | Minus sign | b=4;-b | 4 |
+ | Add | 5+5 | 10 |
- | reduce | 6-4 | 2 |
* | ride | 3*4 | 12 |
/ | except | 5/5 | 1 |
% | modulus ( Remainder ) | 7%5 | 2 |
+ | 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==3 | false |
!= | It's not equal to | 4!=3 | true |
< | Less than | 4<3 | false |
> | Greater than | 4>3 | true |
<= | Less than or equal to | 4<=3 | false |
>= | Greater than or equal to | 4>=3 | true |
- 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 !(*゚ヮ゚)*
边栏推荐
- Tester's algorithm interview question - find mode
- [IELTS reading] Wang Xiwei reads P4 (matching2 paragraph information matching question [difficult])
- Paper notes multi UAV collaborative monolithic slam
- 图解网络:什么是网关负载均衡协议GLBP?
- GDB常用命令
- 1189. Maximum number of "balloons"
- [error reporting] "typeerror: cannot read properties of undefined (reading 'split')“
- ORB(Oriented FAST and Rotated BRIEF)
- NPM install error forced installation
- P4408 [noi2003] truant children (tree diameter)
猜你喜欢
Some basic functions of enterprise projects are developed, and important things are saved to online first a
[paper reading] Tun det: a novel network for meridian ultra sound nodule detection
XML的解析
Illustrated network: what is gateway load balancing protocol GLBP?
How to do the project of computer remote company in foreign Internet?
js如何实现数组转树
Data on the number of functional divisions of national wetland parks in Qinghai Province, data on the distribution of wetlands and marshes across the country, and natural reserves in provinces, cities
2022.07.03(LC_6111_统计放置房子的方式数)
Hisilicon 3559 universal platform construction: YUV422 pit stepping record
Fast analysis -- easy to use intranet security software
随机推荐
js如何实现数组转树
JS convert pseudo array to array
Sorting selection sorting
分布式BASE理论
What did I pay for it transfer to testing post from confusion to firmness?
P4281 [ahoi2008] emergency assembly / gathering (LCA)
模板的进阶
How to effectively monitor the DC column head cabinet
Hologres Query管理及超时处理
Basic points of the game setup of the points mall
基本放大电路的学习
Recursive execution mechanism
Build your own minecraft server with fast parsing
URLs and URIs
跨域请求
Is it safe to open an account in the College of Finance and economics? How to open an account?
【路径规划】RRT增加动力模型进行轨迹规划
圖解網絡:什麼是網關負載均衡協議GLBP?
Huawei employs data management experts with an annual salary of 2million! The 100 billion market behind it deserves attention
Relationship between classes and objects