Scala Basics and Syntaxes

In this tutorial, we are going to learn about the basics of scala programming like basic scala concepts, and syntaxes of writing codes in scala. Submitted by Shivang Yadav Last updated : April 02, 2023

After installing and knowing about Scala, it time to hop on to some basic concepts that are important in Scala. You will use them many times in your Scala development carrier.

Scala basic concepts

  1. OBJECT:
    An object is an instance of a class. it has states and behavior. For Example, A car has states: color, model, number of seats and behaviors: driving, speed.
  2. Class:
    A class is a blueprint that defines the states and behaviors related to it.
  3. Methods:
    It defines the behavior of the objects. It contains logic that are to be performed on the variables.
  4. FIELDS:
    It defines the states of objects. These are variables that a class uses to store its data.
  5. CLOSURE:
    It is a function whose value depends on the value of multiple variables that are declared outside the function. These variables must be either parameters or in the scope of the variable.
  6. TRAITS:
    Traits are just like classes they have fields and methods but multiple traits can be used together(inherited) to increase the functionality of a class.

Scala - Syntax

There is some basic syntax that needs to be addressed before writing our first program to make sure it compiles and run flawlessly.

  • No need of semicolon ; (it optional).
  • Scala does not require any datatype or return type while initialization. Use var for variable and def for function declaration instead.
  • Naming in Scala, Scala is case sensitive. And valid characters are alphabets, digits and _.
    • Class: use camel case with the first letter of every word capital. Example, MyFirstClass
    • Function: use camel case with the first letter of every word (except the first word) capital. Example, myFirstFunction.
    • Filename: it must be the same as the name of the class.
  • Main Function in Scala: The main function is defined as,
  •     def main(args: Array[String])
    

Write your first Scala program

Programming in Scala is easy and you can write your program in either way

  1. Using terminal (interactive method)
  2. Using Notepad (Script method)

1) Using terminal (Interactive Method)

In this method, we code directly in the terminal and execute the code by hitting enter. Multiline code separated by ;.

Steps to write code in the terminal

Step 1: Open Terminal → Type command

    \>scala 

Step 2: Your Terminal will display the version of Scala with some instructions.

Step 3: Now, a > icon will appear. Code your stuff there and hit enter to view the output.

2) Using Notepad (Script Method)

In this method, we will code in a file and then compile it and run to see the output. This method is generally preferred as coding is easy and the saved copy is maintained in case of failure.

Steps to write the code in Notepad

Step 1: Open your notepad and code your Scala program

object HelloWorld {
	def main(args: Array[String]) {
		println("Hello, world! This is my first Scala Program"); 
	}
}

Step 2: Save your code with an extension ".scala" : myFile.scala.

Step 3: Open your terminal and type in the following command and instead of myFile.scala enter the name your file.

    \> scalac HelloWorld.scala
    \> scala HelloWorld

Step 4: View your output as you require.

In my case output is, Hello, world! This is my first Scala Program






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.