Repository navigation
Scala
raghusumanth edited this page May 31, 2022
·
9 revisions
Scala Programming: Scala combines object-oriented and functional programming in one concise, high-level language. Scala's static types help avoid bugs in complex applications, and its JVM and JavaScript runtimes let you build high-performance systems with easy access to huge ecosystems of libraries. Intro: https://docs.scala-lang.org/tour/tour-of-scala.html
Scala is a modern multi-paradigm programming language designed to express common programming patterns in a concise, elegant, and type-safe way.
smoothly integrates the features of object-oriented and functional languages.
Scala is a hybrid functional programming language.
Scala is compiled to run on the Java Virtual Machine.
Scala is pure object-oriented language in the sense that every value is an object.
Types and behavior of objects are described by classes and traits which will be explained in subsequent chapters.
Classes are extended by subclassing and a flexible mixin-based composition mechanism as a clean replacement for multiple inheritance.
Scala is functional:
Scala is also a functional language in the sense that every function is a value and every value is an object so ultimately every function is an object.
Scala provides a lightweight syntax for defining anonymous functions, it supports higher-order functions, it allows functions to be nested, and supports currying. These concepts will be explained in subsequent chapters.
Scala is statically typed
Scala, unlike some of the other statically typed languages (C, Pascal, Rust, etc.), does not expect you to provide redundant type information. You don't have to specify a type in most cases, and you certainly don't have to repeat it.
Scala runs on the JVM
Scala is compiled into Java Byte Code which is executed by the Java Virtual Machine (JVM). This means that Scala and Java have a common runtime platform. You can easily move from Java to Scala.
The Scala compiler compiles your Scala code into Java Byte Code, which can then be executed by the 'scala' command. The 'scala' command is similar to the java command, in that it executes your compiled Scala code.
Scala can Execute Java Code:
Scala enables you to use all the classes of the Java SDK and also your own custom Java classes, or your favorite Java open source projects.
Scala can do Concurrent & Synchronize processing:
Scala allows you to express general programming patterns in an effective way. It reduces the number of lines and helps the programmer to code in a type-safe way. It allows you to write codes in an immutable manner, which makes it easy to apply concurrency and parallelism (Synchronize).
Scala vs Java
Scala has a set of features that completely differ from Java. Some of these are −
All types are objects
Type inference
Nested Functions
Functions are objects
Domain specific language (DSL) support
Traits
Closures
Concurrency support inspired by Erlang
Scala Web Frameworks
Scala is being used everywhere and importantly in enterprise web applications. You can check a few of the most popular Scala web frameworks −
The Lift Framework
The Play framework
The Bowler framework
Environment requirement:
JDK1.8+
set JAVA_HOME
Add JAVA_HOME/bin to Path
verify scala version: scala -version
the ';' line end character is optional in scala
Object − Objects have states and behaviors. An object is an instance of a class. Example − A dog has states - color, name, breed as well as behaviors - wagging, barking, and eating.
Class − A class can be defined as a template/blueprint that describes the behaviors/states that are related to the class.
Methods − A method is basically a behavior. A class can contain many methods. It is in methods where the logics are written, data is manipulated and all the actions are executed.
Fields − Each object has its unique set of instance variables, which are called fields. An object's state is created by the values assigned to these fields.
Closure − A closure is a function, whose return value depends on the value of one or more variables declared outside this function.
Traits − A trait encapsulates method and field definitions, which can then be reused by mixing them into classes. Traits are used to define object types by specifying the signature of the supported methods.