Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Good programmer big data shares the basics of the Scala series

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Good Programmer Big Data Learning Route Sharing Scala Series Basics Understanding Scala

1.1 Scala is short for Scalable Language, a multi-paradigm programming language.

Scala is designed to integrate features of object-oriented and functional programming. Scala runs on the Java platform (Java Virtual Machine) and is compatible with existing Java programs.

Functional programming has two concepts: it is a programming paradigm with a high degree of abstraction. Functions written in purely functional programming languages have no variables. Therefore, any function, as long as the input is determined, the output is determined. This pure function is called no side effect. Programming languages that allow variables to be used have side effects because the state of the variables inside the function is uncertain and the same input may get different outputs.

One of the characteristics of functional programming is that functions are values, allowing the function itself to be passed as an argument to another function, and allowing a function to be returned!

1.2 Scala is object-oriented.

Scala is a pure object-oriented language, where all values are objects, classes and object behavior are described in terms of classes and traits.

Scala is functional.

Scala functions are higher citizens, and all functions are values.

Scala is statically typed.

Scala is scalable.

1.3 Why Scala 1. Elegance: This is the first question framework designers should consider. The users of the framework are application developers. Whether the API is elegant directly affects the user experience.

2. Fast: Scala language expression ability is strong, a line of code is equivalent to Java multi-line, fast development speed;Scala is statically compiled, so it is much faster than JRuby and Groovy.

3. Integration into the Hadoop ecosystem: Hadoop is now the de facto standard for big data, and Spark is not about replacing Hadoop, but improving the Hadoop ecosystem. Most JVM languages probably think of Java, but Java makes an ugly API, or it takes too much effort to implement an elegant API.

2 Environment Preparation 2.1 JDK Installation Because Scala runs on the JVM platform, install JDK before installing Scala

2.2 Scala installation Download Scala and unzip Scala to the specified directory

tar -zxvf scala-2.10.6.tgz -C /usr/java

Configure environment variables and add scala to PATH

vi /etc/profile

export JAVA_HOME=/usr/java/jdk1.7.0_45

export PATH=$PATH:$JAVA_HOME/bin:/usr/java/scala-2.10.6/bin

2.3. Scala development tools installation At present, there are two main Scala development tools: Eclipse and IDEA, both of which have corresponding Scala plug-ins. If you use Eclipse, you can download it directly from Scala's official website.

Because IDEA Scala plug-in is better, most Scala programmers choose IDEA, you can download the free version of the community, click Next to install, if there is a network to install Scala plug-in online. Here we use the Scala plugin installed offline:

1. Install IDEA and click Next. Since we installed the plugin offline, click Skip All and Set Defaul

2. Download scala plugin for IEDA

3. Install Scala plugin: File -> settings -> Plugins -> Install plugin from disk -> Select Scala plugin-> OK -> Restart IDEA

3. Scala Basics 3.1. Values and variables Declare a variable using var.

Scala is a strongly typed language.

var modified variables, content and references are variable

Declare a constant or value using val

val modifies variables that are immutable, noting that immutable is not content, but reference;

val modified variables, equivalent to Java final modified variables;

Only val-qualified variables can be lazy-qualified; once a variable is defined with lazy, its value is instantiated only when the variable is called. And inert variables can only be invariant variables;

//Variable definition

var a:int = 1

var aa = 1

val aaa:Int = 3

val aaaa = 4

lazy val aaaaa = 4

The official recommendation is val.

2. val and var are different:

You can use arrays, which is easier to understand.

class A(n: Int) {

var value = n

}

class B(n: Int) {

val value = new A(n)

}

object Test {

def main(args: Array[String]) {

val x = new B(5)

x = new B(6) //Error?

x.value = new A(6) //Error?

x.value.value = 6 //Normal

}

}

3.lazy

Similar methods, declared first, called later

4.val or var

Under the condition that val and var can be used, val is officially recommended.

3.2. Scala, like Java, has seven numeric types: Byte, Char, Short, Int, Long, Float, Double, and a Boolean type.

Unlike Java, Scala does not have primitive types and wrapper types; these types are classes with their own properties and methods.

Java equivalent wrapper class;

1.toString()

1.to (10)

Scala type hierarchy:

All values in scala have types, both numeric and functional.

Scala type conversion:

val x: Long = 987654321

val y: Float = x // 9.8765434E8 (note that some precision is lost in this case)

val face: Char = '☺'

val number: Int = face // 9786

3.3. Operators Scala has no operators, just method calls in operator format.

//mathematical operators

+、-、* 、/、%

//relational operator

>

< >

=

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report