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 = ???
}
It declares an anonymous (inaccessible) class that extends both B and C, and creates a single instance of this class named 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:
 
 

Scala note 1的更多相关文章

  1. Spark开发环境搭建(IDEA、Scala、SVN、SBT)

    软件版本 软件信息 软件名称 版本 下载地址 备注 Java 1.8 https://www.oracle.com/technetwork/java/javase/downloads/jdk8-dow ...

  2. How to merge Scala Lists

    Scala List FAQ: How do I merge a List in Scala? NOTE: I wrote the solutions shown below a long time ...

  3. Scala: Types of a higher kind

    One of the more powerful features Scala has is the ability to generically abstract across things tha ...

  4. <译>Spark Sreaming 编程指南

    Spark Streaming 编程指南 Overview A Quick Example Basic Concepts Linking Initializing StreamingContext D ...

  5. Beginning Scala study note(9) Scala and Java Interoperability

    1. Translating Java Classes to Scala Classes Example 1: # a class declaration in Java public class B ...

  6. Beginning Scala study note(8) Scala Type System

    1. Unified Type System Scala has a unified type system, enclosed by the type Any at the top of the h ...

  7. Beginning Scala study note(7) Trait

    A trait provides code reusability in Scala by encapsulating method and state and then offing possibi ...

  8. Beginning Scala study note(6) Scala Collections

    Scala's object-oriented collections support mutable and immutable type hierarchies. Also support fun ...

  9. Beginning Scala study note(5) Pattern Matching

    The basic functional cornerstones of Scala: immutable data types, passing of functions as parameters ...

随机推荐

  1. 【外文翻译】 为什么我要写 getters 和setters

    原文作者: Shamik Mitra 原文链接:https://dzone.com/articles/why-should-i-write-getters-and-setters 当我开始我的java ...

  2. linux网络编程(socket)之面向连接(TCP/IP)

    1.流程 服务器: 创建socket: 绑定端口: 监听: 监听到有连接请求,接受请求: 建立连接,开始对话. 客户端: 创建socket: 请求建立连接: 连接建立成功,开始对话. 2.实例代码 / ...

  3. NodeJS 事件循环

    Node.js 是单进程单线程应用程序,但是通过事件和回调支持并发,所以性能非常高. Node.js 的每一个 API 都是异步的,并作为一个独立线程运行,使用异步函数调用,并处理并发. Node.j ...

  4. [CTSC2008] 网络管理

    题目描述 Description M公司是一个非常庞大的跨国公司,在许多国家都设有它的下属分支机构或部门.为了让分布在世界各地的N个部门之间协同工作,公司搭建了一个连接整个公司的通信网络.该网络的结构 ...

  5. [进程管理] Linux中Load average的理解

    Load average的定义 系统平均负载被定义为在特定时间间隔内运行队列中的平均进程树.如果一个进程满足以下条件则其就会位于运行队列中: - 它没有在等待I/O操作的结果 - 它没有主动进入等待状 ...

  6. VMWare下ubuntu无法全屏的问题解决

    遇到的情况: 在VMWare中,安装ubuntu 最新版操作系统(16.04).运行该系统,发现ubuntu系统在虚拟机中,只能居中显示,全屏也只能占一半显示屏幕.怎么看,怎么不舒服. 分析问题: 一 ...

  7. 纯净CentOS7.2 yum源配置与使用yum 安装系统工具net-tools

    本节我们来讲CentOS 的yum 源配置 一.yum 简介 yum,是Yellow dog Updater, Modified 的简称,是杜克大学为了提高RPM 软件包安装性而开发的一种软件包管理器 ...

  8. spring计划任务

    Spring3中加强了注解的使用,其中计划任务也得到了增强,现在创建一个计划任务只需要两步就完成了: 创建一个Java类,添加一个无参无返回值的方法,在方法上用@Scheduled注解修饰一下: 在S ...

  9. 解析Excel文件并把数据存入数据库

    前段时间做一个小项目,为了同时存储多条数据,其中有一个功能是解析Excel并把其中的数据存入对应数据库中.花了两天时间,不过一天多是因为用了"upload"关键字作为URL从而导致 ...

  10. struts2总体介绍

    这篇博客开始将总结一下有关框架的知识,在开发中合适的利用框架会使我们的开发效率大大提高.当今比较流行的开源框架: 关注数据流程的MVC框架 (Struts1/2, WebWork, Spring MV ...