Scala Basic Syntax
If you were previously a Java programmer and understood the basic knowledge of the Java language, then you can quickly learn the basic syntax of Scala.
The biggest difference between Scala and Java is that the semicolon ; at the end of Scala statements is optional.
We can consider Scala programs as a collection of objects that achieve message passing by calling each other's methods. Next, let's understand the concepts of classes, objects, methods, and instance variables:
-
Objects - Objects have properties and behaviors. For example, the state properties of a dog include: color, name, and behaviors include: barking, running, eating, etc. An object is an instance of a class.
-
Classes - A class is an abstraction of an object, and an object is a specific instance of a class.
-
Methods - Methods describe basic behaviors, and a class can contain multiple methods.
-
Fields - Each object has its own unique set of instance variables, i.e., fields. The properties of an object are created by assigning values to fields.
The First Scala Program
Interactive Programming
Interactive programming does not require the creation of script files and can be invoked with the following command:
$ scala
Welcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_31).
Type in expressions to have them evaluated.
Type :help for more information.
scala> 1 + 1
res0: Int = 2
scala> println("Hello World!")
Hello World!
scala>
Script Form
We can also execute code by creating a file called HelloWorld.scala. The code for HelloWorld.scala is as follows:
Code for HelloWorld.scala File:
object HelloWorld {
/* This is my first Scala program
* The following program will output 'Hello World!'
*/
def main(args: Array[String]) = {
println("Hello, world!") // Output Hello World
}
}
Next, we compile it with the scalac command:
$ scalac HelloWorld.scala
$ ls
HelloWorld$.class HelloWorld.scala
HelloWorld.class
After compilation, we can see that the HelloWorld.class file is generated in the directory, which can be run on the Java Virtual Machine (JVM).
After compilation, we can execute the program with the following command:
$ scala HelloWorld
Hello, world!
Basic Syntax
The following points should be noted in the basic syntax of Scala:
-
Case Sensitivity - Scala is case-sensitive, which means that identifiers Hello and hello will have different meanings in Scala.
-
Class Names - The first letter of all class names should be capitalized. class MyFirstScalaClass
-
Method Names - The first letter of all method names should be lowercase. def myMethodName()
-
Program Filename - The name of the program file should match the object name exactly (not required in newer versions, but it is recommended to keep this habit).
-
def main(args: Array[String]) - The Scala program starts processing from the main() method, which is the mandatory program entry part of every Scala program.
Identifiers
Scala can use two forms of identifiers: alphanumeric and symbolic.
Alphanumeric identifiers start with a letter or underscore, followed by letters or numbers. The symbol "$" is also considered a letter in Scala. However, identifiers starting with "$" are reserved for Scala compiler-generated identifiers, and applications should avoid using identifiers starting with "$" to prevent conflicts.
Scala's naming convention follows a camel case similar to Java, with the first character in lowercase, such as toString. The first character of class names is still capitalized. In addition, it is also recommended to avoid using identifiers ending with an underscore to avoid conflicts. Symbolic identifiers contain one or more symbols, such as +, :, ?, etc., for example:
+ ++ ::: < ?> :->
Scala internally uses escaped identifiers, such as:-> represented by $colon$minus$greater. Therefore, if you need to access the:-> method in Java code, you need to use Scala's internal name $colon$minus$greater.
Mixed identifiers consist of an alphanumeric identifier followed by one or more symbols, such as unary_+ for the internal implementation of the + method in Scala. Literal identifiers are defined using ", such as x
yield
.
You can use any valid Scala identifier between ", and Scala will interpret them as a Scala identifier. A typical use is the yield method of Thread. In Scala, you cannot use Thread.yield() because yield is a keyword in