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

1. First acquaintance of scala

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

Share

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

Brief introduction

   Scala (short for Scala Language) is a general programming language that can run on JVM and .net platforms. It can be used for both large-scale application development and scripting.

   Scala was developed by Martin Odersk in 2001 and began to run on JVM and .net platforms in 2004. It has attracted attention because of its concise, elegant and type-safe programming model.

   did not attract much attention at the beginning of Scala. With the rise of big data framework based on Scala such as Apache Spark and Apache Kafka, Scala has gradually come into the eyes of big data practitioners. Proponents of Scala argue that the main advantages of Scala are speed and its expressiveness.

   currently uses scala as the supporting language for the company's development, including Foursquare and Twitter. In 2009, Twitter changed the development language of most background systems from Ruby to Scala.

Feature object-oriented

   scala is a pure object-oriented language, and every value is an object. The data types and behaviors of objects are described by classes and attributes.

Functional programming

   Scala is a functional language whose functions can also be used as values. Scala provides lightweight syntax for defining anonymous functions, supports higher-order functions, allows nesting of multi-tier functions, and supports Corialization.

Static type

   Scala has a type system that ensures code security and consistency through compile-time checking. The type system specifically supports the following features:

Upper and lower bounds of generic class covariant and inverter annotation type parameters explicitly specify type view polymorphic method extensibility when classes and abstract types are referenced as object member compound types

   Scala provides many unique language mechanisms to easily and seamlessly add new language structures in the form of libraries:

Any method can be used as a prefix or suffix operator to automatically construct closure concurrency based on the expected type

   Scala uses Actor as its concurrency model, and Actor is a threaded entity that sends and receives messages through mailboxes.

   Actor can reuse threads, so you can use millions of Actor in your program, while threads can only create a few thousand. In versions later than 2.10, Akka was used as its default Actor implementation.

Programming mode interactive programming

   interactive programming does not need to create a script file and can be invoked with the following command.

$scalaWelcome to Scala version 2.11.7 (Java HotSpot (TM) 64-Bit Server VM, Java 1.8.0x31). Type in expressions to have them evaluated.Type: help for more information.scala > 1 + 1res0: Int = 2scala > println ("Hello World!") Hello WorldScala > script form

  creates a HelloWorld.scala file to execute the code

Object HelloWorld {/ * this is my first Scala program * the following program will output 'HelloWorld!' * / def main (args: Array [String]) {println ("Hello, world!") / / output HelloWorld}}

Next we compile it using the scalac command:

$scalac HelloWorld.scala $lsHelloWorld$.class HelloWorld.scalaHelloWorld.class

Use the following command to execute the program

$scala HelloWorldHello, world! Basic grammar

Case-sensitive: Scala is case-sensitive, which means that identifying Hello and hello have different meanings in Scala.

Class name: capitalize the first letter of all class names. If you need to use several words to form the name of a class, the first letter of each word should be capitalized. Example: class MyFirstScalaClass

Method name: the first letter of all method names is lowercase. If several words are used to form the name of a method, the first letter of each word should be capitalized. Example: def myMethodName ()

Def main (args: Array [String]): the Scala program starts processing with the main () method, which is the mandatory entry part of each Scala program. Keyword

Annotation

   Scala supports both single-line and multiline comments similar to Java. Multiline comments can be nested, but they must be nested correctly, with a comment start symbol corresponding to an end symbol. Comments are ignored in Scala compilation. Examples are as follows:

Object HelloWorld {/ * this is a Scala program * this is an one-line comment * multiline comment * / def main (args: Array [String]) {/ / output HelloWorld / / this is a single-line comment println ("Hello, world!")}} variable

In    Scala, declare variables with the keyword "var" and constants with the keyword "val".

Val myVar: String = "Foo" var myVar: String = "Too"

When declaring variables and constants in Scala,    does not necessarily specify the data type, which is inferred from the initial value of the variable or constant without specifying the data type.

   so, if you declare a variable or constant without specifying the data type, you must give its initial value, otherwise an error will be reported.

Var myVar = 10 + Val myVal = "Hello, Scala!"

Declare multiple variables

/ / xmax, ymax are all declared as 100val xmax, ymax = 100

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