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,

1 + 2 * 3 / x = (1).+(((2).*(3))./(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(100)
res3: Int = 10000

 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> 1 + 1
res1: Int = 2
scala> res1*2 # Any variables you created are available for the lifetime of your session
res3: Int = 4
scala> val x="Hello World" # Scala interpreter infers the value type for you
x: String = Hello World
scala> var x1=x.length
x1: Int = 11
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 03 22:52:01 CST 2016
scala> val str="1_2_3"
str: String = 1_2_3
scala> str.split("_")
res4: Array[String] = Array(1, 2, 3)
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 = 5
if(v==5) println("true") else println("false")

// Exiting paste mode, now interpreting.

true
v: Int = 5
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 <-1 to 10} print(i+" ")
1 2 3 4 5 6 7 8 9 10 
scala> for{i<-1 to 10
| j <- 1 to 10} print(i*j+" ")

 


 

posted @   mengrennwpu  阅读(215)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示