1. What is Scala
Scala is scalable language. It supports in one language object-oriented (like Java) and functional (like Haskell) programming paradigm. It is very practical language for Java virtual mashine (JVM). We can use any existing java code from java world and at the same time we can stay in excelent minimalistic syntax and functional paradigm.
Today Scala has a very rich ecosystem, including several web frameworks such as Play Framework, actor system with Akka library, ORM Slick and more. Scala is a strong static-typed language. It was created in 2001 at EPFL by Martin Odersky. Scala addresses several criticisms of the Java language, and delivers a better developer experience through less code and more concise programs, without losing performance.
Scala and Java share the same infrastructure (JVM), but in terms of design, Scala is a different language in comparison with Java. Java is an imperative object-oriented language and Scala is a post-functional, multiparadigm programing language. Functional programming (FP) works with different principles than object-oriented programming (OOP). FP has grown a lot in the last 10 years. Most of the new languages are pure functional, post-functional, or hybrid (like Java 8).
2. Your First Scala Program
// package name package app object Main { // the main method - entry point in program def main(args: Array[String]): Unit = println("Hello world!") }
In Scala IDE menu: File -> New -> Scala project -> Enter "Project name" (first) -> Finish button
Click on "first" in the Package Explorer(left column) -> Right button mouse click (run context menu) on "src" -> New -> Package -> Enter "Name" (app) -> Finish button
Right button mouse click on "app" package -> New -> Scala object -> Enter "Name" (app.Main)
Enter above main method in object Main
Another way is to extend object from App trait
package app object Main extends App { println("Hello world!") }
3. Variables, Constants and Strings
Declaration of mutable variables:
var x = 10 // variable x of type Int value 10 x = 11 x += 1 // we can change x
Declaration of immutable variables (constants):
val y = 10 // constant y of type Int value 10 y = 11 // error: reassignment to val
Declaration with type:
val z: Double = 10 // constant z of type Double value 10.0 val greeting: String = null val greeting1: Any = "Hello"
Examples with several variables in line:
val xmax, ymax = 100 val a0 = 1; var b0 = 3; val c0 = 2.4 val (a, b, c) = (5, "Hello", 3.14) // declaration as Tuple
Strings declaration:
val s1 = "String" val s2 = "My " + s1 // concatenation val s3 = s1 + 100 // concatenation with number val s4 = 100.toString val s5 = """first line second line""" // multiline string
Strings are java.lang.String:
"".isEmpty // true "abc".getBytes // Array(97, 98, 99) "abc".charAt(1) // b "abc".length // 3
Strings are Seq (scala List abstraction):
"Hello".intersect("world") // lo "Hello" intersect "world" // lo "aabbbccc".distinct // abc "abcde"(3) // d "abcde".head // a "abcde".size // 5 "abcde".reverse // edcba "aBcDe".filter(x => x.isUpper) // BD
Автор (*): | Город: | ||
Эл.почта: | Сайт: | ||
Текст (*): | |||