Beginning Scala study note(1) Geting Started with Scala
1. Scala is a contraction of "scalable" and "language". It's a fusion of objected-oritended and functional programming.
2. Compare Book class between Java and Scala
Java:
class Book{
private String title;
private int numberOfPages; public Book(String title, int numberOfPages){
this.title = title;
this.numberOfPages = numberOfPages;
}
}
Scala:
class Book(title: String, numberOfPages : Int)
3. Scala does not include static members, primitive types, break and continue statements, enums, wildcards(通配符). Scala includes features such as type inference(类型推断) and extensible APIs.
4. Case classes don’t have code such as getter/setter, equals,hashCode,toString etc.
case class Book(var title: String, var numberOfPages: Int)
Example:
(1) Compile Book.scala: scalac Book.scala, this creates Book.class and Book$.class file.
(2) disassemble Book.class: javap Book, then illustrates the Java equivalent as follows:
public class Book implements scala.ScalaObject,scala.Product,scala.Serializable {
public static final scala.Function1<scala.Tuple2<java.lang.String, java.lang.Object>, Book> tupled();
public static final scala.Function1<java.lang.String, scala.Function1<java.lang.Object, Book>> curry();
public static final scala.Function1<java.lang.String, scala.Function1<java.lang.Object, Book>> curried();
public scala.collection.Iterator<java.lang.Object> productIterator();
public scala.collection.Iterator<java.lang.Object> productElements();
public java.lang.String title();
public void title_$eq(java.lang.String);
public int numberOfPages();
public Book copy(java.lang.String, int);
public int copy$default$2();
public java.lang.String copy$default$1();
public int hashCode();
public java.lang.String toString();
public boolean equals(java.lang.Object);
public java.lang.String productPrefix();
public int productArity();
public java.lang.Object productElement(int);
public boolean canEqual(java.lang.Object);
public Book(java.lang.String, int);
}
5. Everything in Scala is an object, including numbers. Thus,
+ * / x = ().+((().*())./(x))
6. Scala traits are similar with Java interfaces, the difference is that traits can include method implementations. Scala also does not support static members, instead a Scala class can provide a singleton object. A singleton object uses object keyword.
object HelloWorld{
def greet(){
println("Hello World!")
}
}
HelloWorld is a singleton object. Call the method like HelloWorld.greet()
7. In Scala, you can pass functions to methods and functions, and return them from methods and functions, as well as assign them to variables. Functions also are objects.
(i: Int) => {i * i}
It defines a function that takes an Int parameter and returns a value that is square of the provided Int.
Assign the function to a variable: val square = (i: Int) => { i * i }, square is an instance of a function
Example:
scala> val square = (i: Int) => {i*i}
square: Int => Int = <function1>
scala> square()
res3: Int =
8. Scala can call any Java code, subclass any Java class, and implement any Java interface. Scala code reuses Java libraries and Java types. Scala was designed for seamless interoperability(互通性) with Java and ultimately Scala programs compile to JVM bytecode.
9. Intsall Scala:
Add these lines to your $HOME/./bash_profile:
export SCALA_HOME=/Users/Al/scala
PATH=$PATH:/Users/A1/scala/bin
10. scala offers different ways to run programs:
(1) Interactive at a REPL command line.
scala> println("Hello World!")
Hello World!
scala> +
res1: Int =
scala> res1* # Any variables you created are available for the lifetime of your session
res3: Int =
scala> val x="Hello World" # Scala interpreter infers the value type for you
x: String = Hello World
scala> var x1=x.length
x1: Int =
scala> import java.util._ # java.util library in session we can use
import java.util._
scala> val d = new Date
d: java.util.Date = Fri Jun :: CST
scala> val str="1_2_3"
str: String = 1_2_3
scala> str.split("_")
res4: Array[String] = Array(, , )
scala> :help # help命令
scala> :paste # Multiline paste mode supports entering multiple lines of code to be compiled together, and external source code and libraries
// Entering paste mode (ctrl-D to finish) val v =
if(v==) println("true") else println("false") // Exiting paste mode, now interpreting. true
v: Int =
scala> :quit
(2) scala scripts
Type scala code into a text file and save it with an extension .scala.
println("Hello World!")
scala Hello.scala
Hello World
(3) Compiling Scala Programs
> scalac File1.scala File2.scala # compile Scala source files into class files
> fsc File1.scala File2.scala # use fast scala compiler, which is very useful smaller projects.
SBT is an open source build tool for Scala and Java projects, which provides native support for compiling Scala code and integrating with many Scala test frameworks and dependency management, continuous compilation, testing, and deployment.
11. Scala programs
(1) HelloWorld. Remark: A semicolon at the end of a statement is usually optional.
object HelloWorld{
def main(args: Array[String]){
println("Hello World!")
}
}
main method is defined in an object, not in class. Actually return type is Unit, which is similar to void.
Specify the return type:
def main(args :Array[String]) : Unit = { } # def: tell comppiler that this is a method
(2) Print1.scala
scala> for{i <- to } print(i+" ") scala> for{i<- to
| j <- to } print(i*j+" ")
Beginning Scala study note(1) Geting Started with Scala的更多相关文章
- Beginning Scala study note(3) Object Orientation in Scala
1. The three principles of OOP are encapsulation(封装性), inheritance(继承性) and polymorphism(多态性). examp ...
- Beginning Scala study note(4) Functional Programming in Scala
1. Functional programming treats computation as the evaluation of mathematical and avoids state and ...
- 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 ...
- Beginning Scala study note(2) Basics of Scala
1. Variables (1) Three ways to define variables: 1) val refers to define an immutable variable; scal ...
- 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 ...
- Beginning Scala study note(6) Scala Collections
Scala's object-oriented collections support mutable and immutable type hierarchies. Also support fun ...
- Beginning Scala study note(5) Pattern Matching
The basic functional cornerstones of Scala: immutable data types, passing of functions as parameters ...
- Beginning Scala study note(7) Trait
A trait provides code reusability in Scala by encapsulating method and state and then offing possibi ...
- Scala 深入浅出实战经典 第49课 Scala中Variance代码实战(协变)
王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-64讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...
随机推荐
- display:none显示和隐藏
<html> <head> <title>显示和隐藏问题</title> <meta charset="utf-8"/> ...
- HTML Select 标签选择后触发jQuery事件代码实例
页面设计原由: 因为很多客户不知道如何来到我们公司,领导想让我在微信公众号上面做一个链接,客户可以直接通过微信公众号打开地图并导航到我们公司的办公地点. 实现起来并不难,但由于公司有很多办事处,所以需 ...
- R语言:ggplot2精细化绘图——以实用商业化图表绘图为例
本文版权归http://www.cnblogs.com/weibaar 本文旨在介绍R语言中ggplot2包的一些精细化操作,主要适用于对R画图有一定了解,需要更精细化作图的人,尤其是那些刚从exce ...
- [Algorithm] 局部敏感哈希算法(Locality Sensitive Hashing)
局部敏感哈希(Locality Sensitive Hashing,LSH)算法是我在前一段时间找工作时接触到的一种衡量文本相似度的算法.局部敏感哈希是近似最近邻搜索算法中最流行的一种,它有坚实的理论 ...
- NSIS检测.NET Framework并在线下载
Section -.NET Framework ;检测是否是需要的.NET Framework版本 Call GetNetFrameworkVersion Pop $R1 ;${If} $R1 < ...
- jquery.nicescroll.js可全屏可改滚动条颜色的滚动条插件-推荐
有一款很棒的插件 http://www.ijquery.cn/?p=666
- Sql Server函数全解<三>数据类型转换函数和文本图像函数
阅读目录 一:数据类型转换函数 二:文本和图像函数 一:数据类型转换函数 在同时处理不同数据类型的值时,SQL Server一般会自动进行隐士类型转换.对于数据类型相近的值是有效的,比如int和flo ...
- 微信的audio无法自动播放的问题
一.问题 最近做了一个html5的项目,里面涉及到音乐播放,项目要求音乐进入页面就自动播放,于是我就想到了html5的audio标签,将mp3引入进去. 1.在audio标签里引入了autoplay属 ...
- 【ASP.NET】复制单个文件同时到多个目录
有时候,当我们更新了一个dll文件后,需要将该dll文件复制到到不同的文件夹中,手动操作会很麻烦,因此可以考虑利用程序实现. 利用powershell批量复制 示例代码如下: $source=&quo ...
- [MySQL]show index from tb_name命令各列的含义
show index from table_name 这个命令有助于诊断性能低下的查询,尤其是查询是否使用了可用的索引. 下面介绍下 这个命令显示的结果列的含义: | Table | Non_uniq ...