Getting started with Scala--the first section of the Scala language

Source: Internet
Author: User

The main contents of this section
    1. About Scala
    2. Why to learn Scala
    3. Scala language Preliminary
1. Introduction to Scala

The Scala (Scala language abbreviation) language is a common programming language that runs on both the JVM and the. NET platform and can be used for both large-scale application development and scripting, developed by Martin Odersk in 2001. Started in 2004 the program runs on the JVM and the. NET platform, and is being watched for its simplicity, elegance, and type-safe programming patterns.

Scala creator--martin Odersk

At the beginning of Scala's creation, there was little attention, and Scala is emerging as a big data practitioner with the rise of the Scala-based big data framework such as Apache Spark and Apache Kafka. Scala's advocates believe that the main advantage of Scala is speed and its expressive nature. The current use of Scala as a support company for developing languages includes Foursquare and Twitter. In 2009, Twitter changed most of the backend system's development language from Ruby to Scala. See this article: Twitter on Scala:a conversation with Steve Jenson, Alex Payne, and Robey Pointer, "Scalazine, April 3,2009, www.ar Tima.com/scalazine/articles/twitter_on_scala.html.

The Scala language has the following features:

1 Pure object-oriented programming language

    • (1) encapsulation/information hiding.
    • (2) Inheritance.
    • (3) Polymorphism/dynamic binding.
    • (4) All predefined types is objects.
    • (5) All operations is performed by sending messages to objects.
    • (6) All user-defined types is objects.

2 Functional programming languages
Definition: Functional programming is a programming paradigm that treats computation as the evaluation of mathematical D avoids state and mutable data.

Functional programming languages should support the following features:
(1) Higher order function (Higher-order functions)
(2) Closure (closures)
(3) Pattern matching (pattern matching)
(4) Single Assignment (Assignment)
(5) Delay calculation (lazy evaluation)
(6) Type derivation (type inference)
(7) Tail call optimization (Tail called optimization)
(8) Type derivation (type inference)

3 Scala language has strong compatibility, portability
Scala runs on the JVM and can interoperate with Java, with Java-like platform portability

4 Simplicity of Scala grammar
Here is the Java Hadoop wordcount code and spark WordCount code

As you can see, the spark three line of code solves what Hadoop seventy or eighty lines of code do.

2. Why to learn Scala

1 Open source Big Data memory computing framework Spark's popularity

    • Spark is currently the most popular open source Big Data memory computing framework, implemented in the Scala language, developed by UC Berkeley Amplab Labs (2009) and open source in 2010, becoming the top project of the Apache Foundation in 2014 http://spark.apache.org/
    • Spark has a good performance advantage.

      Image source: databricks.com/blog/2014/11/05/spark-officiallysets-a-new-record-in-large-scale-sorting.html
    • Community activity Level

      Photo Source: Real-time Analytics with Spark streaming

      Photo Source: Spark Summit 2015

      Image source: twitter.com/dberkholz/status/568561792751771648

    • Use and contribution of major companies


Photo Source: Summit Spark https://spark-summit.org/2015/
-IBM million data engineer program
"June 17, 2015, Beijing" IBM (NYSE:IBM) announced its commitment to aggressively advancing the Apache Spark Project, saying the project is the most important new open source project in a data-driven, next decade. At the heart of this commitment is the embedding of spark into IBM's industry-leading analytics and business platform, and Spark as a service to customers on the IBM Bluemix platform. IBM will also invest more than 3,500 research and development staff in more than 10 labs around the world to launch spark-related projects and will provide groundbreaking machine learning technology--IBM SYSTEMML for the spark open source ecosystem, while IBM will also train more than 1 million spark data scientists and data engineers. Original link: http://www.csdn.net/article/a/2015-06-18/15825412

2 Scala is the mainstream language for future big data processing

    • It is the development language of the spark framework
      • "If I were to pick a language to use the today other than Java, it would is Scala."-james Gosling

        James Goslin the father of Java
    • Scala has the natural advantage of data processing (language feature decision)

3. Scala language Preliminary

1 variable definition

//Declare a Val variable//Same as a variable declared with the Java final keyword//Once assigned, it cannot be changed//scala will help us with type inference .Scala>Val hellostring="Hello World"Hellostring:String =Hello World//can also be type specifiedScala>Val hellostring:String="Hello World"Hellostring:String =Hello World//string is actually java.lang.String .Scala>Val Hellostring:java.Lang.String="Hello World"Hellostring:String =Hello World//cannot be re-assigned because it is a Val variableScala>Hellostring="Hello Crazy World"<Console>:8: error:reassignment toVal hellostring="Hello Crazy World"^

The following delay-loaded variables are given:

//lazy关键字声明变量//表示该变量不会马上赋值//而在真正被使用时才赋值lazyval helloString="Hello Crazy World"helloString: String = <lazy>//在真正使用时被赋值scala> helloStringres1: String = Hello Crazy World

There are also mutable variables in Scala, where variable content can change dynamically as the program runs:

//var 声明可变变量var helloString="Hello Cruel World"String = Hello Cruel World//重新赋值scala>  helloString="GoodBye Cruel World"String = GoodBye Cruel World

2 function Preliminary

In Scala, function definitions are performed in the following ways:

Defines a function that uses thereturnreturn result scala> defAdd(a: int,b:int): int={return a+B}Add: (a: Int, B:int) intscala>Add(1,2) Res3:int =3//Can save Return,scala will be the last execution statement//As the return value of the functionScala> defAdd(a: int,b:int): int={a+B}Add: (a: int, b:int) int//Omit return value type, Scala automatically makes type inferenceScala> defAdd(a: Int,b:int) ={a+B}Add: (a: Int, B:int) intscala>Add(1,2) Res4:int =3

3 HelloWorld Applications:

package cn.xtwy.scala.chapter01//scala应用程序同样采用main方法作为应用程序的入口object HelloWorld {  def main(args: Array[String]): Unit = {    println("Hello World")  }}

Add a public number to find out more about the latest spark and Scala technical information

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Getting started with Scala--the first section of the Scala language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.