当前位置:网站首页>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 !(*゚ヮ゚)*
边栏推荐
- Ap8022 switching power supply small household appliances ACDC chip offline switching power supply IC
- uniapp上传头像
- Summer challenge brings you to play harmoniyos multi terminal piano performance
- leetcode518,377
- GDB常用命令
- [paper reading] Tun det: a novel network for meridian ultra sound nodule detection
- Acwing164. Accessibility Statistics (topological sorting +bitset)
- 海思3559万能平台搭建:YUV422的踩坑记录
- [paper reading] cavemix: a simple data augmentation method for brain vision segmentation
- If you open an account of Huatai Securities by stock speculation, is it safe to open an account online?
猜你喜欢
华为200万年薪聘请数据治理专家!背后的千亿市场值得关注
【Unity】InputSystem
[paper reading] Tun det: a novel network for meridian ultra sound nodule detection
Hill sort of sorting
Ap8022 switching power supply small household appliances ACDC chip offline switching power supply IC
[论文阅读] CarveMix: A Simple Data Augmentation Method for Brain Lesion Segmentation
Fs8b711s14 electric wine bottle opener MCU IC scheme development special integrated IC
Power operation and maintenance cloud platform: open the new mode of "unattended and few people on duty" of power system
Detailed explanation of openharmony resource management
《论文笔记》Multi-UAV Collaborative Monocular SLAM
随机推荐
[IELTS reading] Wang Xiwei reading P4 (matching1)
Specification for fs4061a boost 8.4v charging IC chip and fs4061b boost 12.6V charging IC chip datasheet
Leetcode70 (Advanced), 322
【雅思阅读】王希伟阅读P4(matching2段落信息配对题【困难】)
Life is changeable, and the large intestine covers the small intestine. This time, I can really go home to see my daughter-in-law...
leetcode518,377
const、volatile和restrict的作用和用法总结
uniapp微信小程序拿来即用的瀑布流布局demo2(方法二)(复制粘贴即可使用,无需做其他处理)
leetcode494,474
他做国外LEAD,用了一年时间,把所有房贷都还清了
The pit of sizeof operator in C language
Acwing164. Accessibility Statistics (topological sorting +bitset)
跨域请求
OpenHarmony资源管理详解
【雅思阅读】王希伟阅读P4(matching1)
[论文阅读] TUN-Det: A Novel Network for Thyroid Ultrasound Nodule Detection
P3304 [sdoi2013] diameter (diameter of tree)
Ap8022 switching power supply small household appliances ACDC chip offline switching power supply IC
The waterfall flow layout demo2 (method 2) used by the uniapp wechat applet (copy and paste can be used without other processing)
lambda expressions