
What is Scala?
Scala stands for Scalable Language. It is a multi-paradigm programming language. Scala language has features of functional programming and object-oriented programming. It is a statically typed language. Scala’s source code is compiled into bytecode and executed by Java virtual machine(JVM). Scala is an immutable-first language. Scala makes it easy to write code using immutable data for developers. This includes constructs such as Val, case class, higher-order function. All default data structures are immutable in Scala.
Why scala focuses on immutablity?
Scala focuses on immutability because it simplifies program logic. If something is mutable, you have to retain an idea of what its state is throughout the flow of the method in the code. That can be difficult—tougher than creating something that never changes and then building some new thing from that. Immutable code is very important in software development.
It allows for effortless parallelism without affecting any thread-safety issues.
The last point is probably the most important. Writing mutable code with parallelism is hard and error-prone. Now, in the modern world, we write everything in parallel and possibly in distributed ways. Immutable functional programming is the only way we have, how to do this safely and easily.
Features of Scala-
- Type inference- Type inference is a Scala feature to infer types of the variable without writing mentioning the type explicitly. The language is smart enough to deduce data types based on the context in which the object has been written in the code.
- Higher-Order Functions- A higher-order function is a function that either takes a function as an argument or returns a function. In other words, we can say a function that works with another function is called a higher-order function.
- Lazy Computation- Scala evaluates an expression when it is required. This increases performance by reducing compile time of the code.
- String Interpolation- Scala’s string interpolation feature allows you to embed variables directly inside a string literal allowing the creation of strings through data.
- Traits- Trait is a collection of abstract and non-abstract methods. They are similar to Java’s interface but powerful than Java’s interface. Normal classes and objects can only inherit from a single class, but classes and objects can inherit from multiple traits.
- Concurrency control- Scala provides a standard library that includes the actor model. You can write concurrency code by using an actor system. Scala provides a platform and tool to deal with concurrency known as Akka. Akka is a separate open-source framework that provides actor-based concurrency.
- Case Classes & Pattern Matching- Scala case classes are just regular classes that are immutable by default and decompose through pattern matching. All the parameters listed in the case class are public and immutable by default by nature. Case classes are decomposed by pattern matching. So, we can able to write more logical code.
Scala vs Java-
Scala | Java | |
1. | Support OOP’s and Functional Programming. | Support only OOP’s. |
2. | Scala variables are by default immutable. | Java variables are mutable types. |
3. | It supports lazy evaluation. | Java doesn’t support lazy evaluation. |
4. | Code is written in a compact form. | Code is written in long form. |
5 | No static members. | Contains static members |
First program in scala(hello world)-
object FirstScalaProgram {
def main(args:Array[String]){
println (“HelloWorld” )
}
}
In the above code, we have created the object FirstScalaProgram. It contains the main method and displays messages using println method.
This file is saved with the name FirstScalaProgram.scala.
Output–
Hello World
Conclusion
In this blog, we have learnt about basic things related to scala. We also get to know about scala features and differences between Scala and Java.