当前位置:网站首页>Fundamentals of scala (3): operators and process control
Fundamentals of scala (3): operators and process control
2022-07-19 11:46:00 【51CTO】
List of articles
One 、 Operator
Scala Operators in and Java The operators in are basically the same .
Arithmetic operations
+ - * / %,+and-Show positive and negative signs in the unary operation table , To express addition and subtraction in a binary operation ./To divide or divide , Keep only integer parts and discard decimal partsbesides ,
+It also means adding two strings
Relationship between operation
== != < > <= >=- stay Java in ,
==Compare the values of the two variables themselves , The first address of two objects in memory ,equalsCompare whether the contents contained in the string are the same . - Scala Medium
==More similar to Java Mediumequals, andeq()The comparison is the address
Logical operations
&& || !, The result of the operation is Boolean value- Scala Short circuit is also supported
&& ||
The assignment operation
= += -= *= /= %=- stay Scala There is no
++and--This grammar , adopt+=、-=To achieve the same effect
An operation
& | ^ ~<< >> >>>, among<< >>Is a signed shift left and right ,>>>unsigned right shift
stay Scala In fact, there is no operator in , All operators are method calls .
- When an object's method is called , spot . It can be omitted
- If there is only one function parameter , Or no parameters ,() It can be omitted
- Operator priority :
Take a chestnut :
Two 、 Process control
Scala Process control in is the same as other programming languages , It also contains branch statements 、 Loop statement, etc .
if - else
Basic grammar :
Take a chestnut :
What's special :
- Different from other languages ,Scala Medium
if elseAn expression actually has a return value , It can also be used as an expression , Defined as the return value of the last statement executed - Scala Inconsistent return value types in , Take their common ancestor type .
- The return value can be
Unittype , At this point, the value of the last expression is ignored , obtain() - scala There is no ternary conditional operator in , It can be used
if (a) b else creplacea ? b : c - Nested branches have the same characteristics .
Take a chestnut :
for
Scala Medium for The loop is called for Derivation
Range traversal :
iRepresents a cyclic variable<-It is equivalent to assigning traversal value to variable1 to 10RichIntA method call in , Return to oneRange- Close back and forth
untilIt's alsoRichIntOne of the methods in , returnRangeclass- Before closed after opening
A range is also a collection , You can also traverse ordinary collections .
Cycle step :by + step
Cycle guard :
Cycle guard , That is, circulation protection ( Also known as conditional judgment , The guards ). The protection type is true Then go inside the circulatory body , by false Then skip , Be similar to continue.
grammar :
Equivalent to :
Take a chestnut :
A nested loop :
Nested loops can combine conditions into one for in .
grammar :
Equivalent to :
Take a chestnut : Print multiplication tables
Loop introduces variables :
The introduced variable in the loop , But not a cyclic variable .
Equivalent to :
Loop return value :
- Scala in for The default return value of the loop is
Unit, example().
- yield:Java One method of threading in is yield Indicates a thread concession
- scala Is a keyword in Express : At present for The loop generates a collection type as the return value , Then return .
- Return the results processed during traversal to a new Vector Collection , Use yield keyword .
while and do…while
While and do…While The use of and Java The usage is the same in language , It is not recommended to use , The result type is Unit.
grammar :
Cycle break
Scala The built-in control structure specifically removes break and continue, In order to better adapt to functional programming , It is recommended to use the functional style to solve break and continue The function of , Not a keyword .Scala Use in breakable Control structure to achieve break and continue function .
Break the loop by throwing an exception
Use scala Medium Breaks Medium break Method ( It is equivalent to encapsulating exception capture ), Throw and catch exceptions .
This is the end of the sharing , I hope to learn from you Scala Language helps !!!
边栏推荐
- High performance IO framework library libevent (III): overview of libevent framework functions
- Keras deep learning practice (14) -- r-cnn target detection from scratch
- Hello JSON Schema
- 03-2、
- Opencv draw a black rectangle and write the serial number
- 03-1、内联函数、auto关键字、typeid、nullptr
- Tidb memory control document
- 开发那些事儿:如何解决RK芯片视频处理编解码耗时很长的问题?
- Introduction to common distributed locks
- 梦想CMS 前台搜索SQL注入
猜你喜欢

委派双亲之类加载器

Redis分布式緩存-Redis集群

Total number of blocking and waiting in jconsole thread panel (RPM)

TCP拥塞控制详解 | 7. 超越TCP

Discussion on Euler angle solution of rocket large maneuvering motion

Introduction to common distributed locks

synchronized锁升级

Lychee sound quality high fidelity AI noise reduction technology sharing

Learning outline of the column "MySQL DBA's magic road"

An error, uncaught typeerror: modalfactory is not a constructor
随机推荐
Leetcode 1328. 破坏回文串(可以,已解决)
Leetcode 1252. Number of odd value cells
委派雙親之類加載器
【嵌入式单元测试】C语言单元测试框架搭建
Why does the magnetic variable speed gear reverse?
03-1、内联函数、auto关键字、typeid、nullptr
TiKV 线程池性能调优
Leetcode 1310. Subarray XOR query
Swift 二进制数据与16进制字符串的相互转换
03-2、
2022 National latest fire-fighting facility operator (intermediate fire-fighting facility operator) simulation test questions and answers
[embedded unit test] construction of C language unit test framework
Leetcode 1304. N different integers with zero and
Redis分布式緩存-Redis集群
Conversion of unity3d model center point (source code)
A curated list of awesome Qt and QML
华为无线设备配置频谱导航
TCP congestion control details | 7 Surpass TCP
STL string输入输出重载
Research on Wenhua commodity index