Scala--commonly used 1
Recently I transit to use scala to program.
scala is a functional and objected oriented language, but it has seamless java Interoperability (they both run in JVM and freely mixed).
Compared to the java that I am familiar to, there are some common concepts, data structure functions I often use in Scala,
They are also some kinds of distinctions from Java object oriented language. I put here also for quick search afterwards.
(1) var: define variable;
val: deine a constant
e.g.
var i = 0; i = i + 1 // i can be changed
val i: Int = 0 //i value is not allowed to change
(2) object
Everything is object;
e.g. even basic data structure Int are interpreted as
abstract final
class
Int(3) difference between object and class:
Simple differences:
Object: A singleton is a class that can have only one instance, it is like the static field and method in the java class , but it can extend another superclass, implement interfaces,
Class is that you can have multiple instances of a class.
e.g.
object A extends B with C {
def f(x: Any): Any = ???
}
B
C
A
(4) Option/Some/None pattern
Scala uses option to avoid the null or null pointer problem in Java etc.
it use values that may be present or not: the
Option[A]
trait.Some
extends Option
, so it inherits everything except get
and isEmpty
(and some other methods implemented by a case class).None also extend option
In a word,
Option
/ \
/ \
/ \
Some None
Option is container base which can be empty or full
While Some(x) represent full with 'x' being present in the container, None represents empty.
val a: Option[String] = Option(null) // a will be None
val b: Option[String] = Option("Hello!") // "hello"
(4) trait
Similar to java's interface, it encapsulates method and field definitions. It can also be used to define object types by specifying the signature of the supported methods.
example:
trait Equal { def isEqual(x : Any) : Boolean def isNotEqual(x : Any) : Boolean = !isEqual(x) } class Point(xc : Int, yc : Int) extends Equal { var x : Int = xc var y : Int = yc def isEqual(obj : Any) = obj.isInstanceOf[Point] && obj.asInstanceOf[Point].x == y } |
(5) case class
It defines abstract or concrete properties in an abstract base class (or trait) that can be referenced in all child classes.
Case classes are compared by structure and not by reference:
case class Message(sender: String, recipient: String, body: String)
val message1 = Message("jorge@catalonia.es", "guillaume@quebec.ca", "Com va?")
You can create a deep copy of an instance of a case class simply by using the
copy
method. You can optionally change the constructor arguments.val message2 = message1.copy(sender = message4.recipient, recipient = "claire@bourgogne.fr")
It can be used to construct the struct data structure in c/c++
reference:
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
· 《HelloGitHub》第 106 期
· 数据库服务器 SQL Server 版本升级公告
· C#/.NET/.NET Core技术前沿周刊 | 第 23 期(2025年1.20-1.26)