当前位置:网站首页>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 !(*゚ヮ゚)*
边栏推荐
- 【C】 (written examination questions) pointer and array, pointer
- leetcode494,474
- A new method for analyzing the trend chart of London Silver
- Significance of acrel EMS integrated energy efficiency platform in campus construction
- If you open an account of Huatai Securities by stock speculation, is it safe to open an account online?
- 企业应用业务场景,功能添加和修改C#源码
- ORB(Oriented FAST and Rotated BRIEF)
- Safety learning week4
- 同事的接口文档我每次看着就头大,毛病多多。。。
- Hologres Query管理及超时处理
猜你喜欢
Ap8022 switching power supply small household appliances ACDC chip offline switching power supply IC
兩個數相互替換
leetcode518,377
公司要上监控,Zabbix 和 Prometheus 怎么选?这么选准没错!
青海省国家湿地公园功能区划数数据、全国湿地沼泽分布数据、全国省市县自然保护区
Sorting selection sorting
《论文笔记》Multi-UAV Collaborative Monocular SLAM
Face recognition 5- insight face padding code practice notes
Power operation and maintenance cloud platform: open the new mode of "unattended and few people on duty" of power system
lambda expressions
随机推荐
Distributed base theory
The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!
基本放大电路的学习
Application of fire fighting system based on 3D GIS platform
[STM32] (I) overview and GPIO introduction
js如何实现数组转树
他做国外LEAD,用了一年时间,把所有房贷都还清了
Acwing164. Accessibility Statistics (topological sorting +bitset)
Relationship between classes and objects
Sorting selection sorting
npm install报错 强制安装
Binary conversion problem
GDB常用命令
基于三维gis平台的消防系统运用
leetcode494,474
企业应用业务场景,功能添加和修改C#源码
Nine Qi single chip microcomputer ny8b062d single key control four LED States
How to do the project of computer remote company in foreign Internet?
Microservice
Best practice case of enterprise digital transformation: introduction and reference of cloud based digital platform system security measures