poltvegan.blogg.se

Scala code
Scala code











Operator characters are printable ASCII characters such as +, :, ?, ~ or #.įollowing are legal operator identifiers − The '$' character is a reserved keyword in Scala and should not be used in identifiers.įollowing are legal alphanumeric identifiers −Īn operator identifier consists of one or more operator characters. Alphanumeric IdentifiersĪn alphanumeric identifier starts with a letter or an underscore, which can be followed by further letters, digits, or underscores. Scala supports four types of identifiers. A keyword cannot be used as an identifier and identifiers are case-sensitive. Names used for objects, classes, variables and methods are called identifiers. Then the file should be saved as 'HelloWorld.scala'.ĭef main(args: Array) − Scala program processing starts from the main() method which is a mandatory part of every Scala Program.Īll Scala components require names.

scala code

(If the file name and the object name do not match your program will not compile).Įxample − Assume 'HelloWorld' is the object name. When saving the file you should save it using the object name (Remember Scala is case-sensitive) and append ‘. Program File Name − Name of the program file should exactly match the object name. If multiple words are used to form the name of the method, then each inner word's first letter should be in Upper Case. Method Names − All method names should start with a Lower Case letter. If several words are used to form a name of the class, each inner word's first letter should be in Upper Case. The following are the basic syntaxes and coding conventions in Scala programming.Ĭase Sensitivity − Scala is case-sensitive, which means identifier Hello and hello would have different meaning in Scala.Ĭlass Names − For all class names, the first letter should be in Upper Case.

scala code

Use the following command to compile and execute your Scala program. This is a bytecode which will run on Java Virtual Machine (JVM) using ‘ scala’ command. One of them will be called HelloWorld.class. The ‘ scalac’ command is used to compile the Scala program and it will generate a few class files in the current directory. Open the command prompt window and go to the directory where the program file is saved. Println("Hello, world!") // prints Hello World * This will print 'Hello World' as the output

#Scala code code

Open notepad and add the following code into it. Use the following instructions to write a Scala program in script mode. Type the following text to the right of the Scala prompt and press the Enter key − Type in expressions to have them evaluated. If Scala is installed in your system, the following output will be displayed − Open the command prompt and use the following command to open Scala. We can execute a Scala program in two modes: one is interactive mode and another is script mode. Traits are used to define object types by specifying the signature of the supported methods. Traits − A trait encapsulates method and field definitions, which can then be reused by mixing them into classes. An object's state is created by the values assigned to these fields.Ĭlosure − A closure is a function, whose return value depends on the value of one or more variables declared outside this function. It is in methods where the logics are written, data is manipulated and all the actions are executed.įields − Each object has its unique set of instance variables, which are called fields.

scala code

Methods − A method is basically a behavior. Example − A dog has states - color, name, breed as well as behaviors - wagging, barking, and eating.Ĭlass − A class can be defined as a template/blueprint that describes the behaviors/states that are related to the class.

scala code

Object − Objects have states and behaviors. Let us now briefly look into what do class, object, methods and instance variables mean. When we consider a Scala program, it can be defined as a collection of objects that communicate via invoking each other’s methods. The biggest syntactic difference between Scala and Java is that the ' ' line end character is optional. If you have a good understanding on Java, then it will be very easy for you to learn Scala.











Scala code