Intro
Scala is a powerful programming language that combines object-oriented and functional programming. It runs on the Java Virtual Machine (JVM) and offers concise syntax, compatibility with Java, and advanced features for building scalable applications.
Variables declaration in scala
Scala primarily support two kind of varaibles,
- val : -- variables declared with val are immutables Ex: val a: String = "Hellow"
- var : -- variables declared with var are mutables, Ex: var b: String = "World"
val a: String = "Hellow" // Immutables
var b: String = "World" // Mutables
In scala we can intialize variable declaration with multiple syntaxes, below are examples
val x: Int = 42 // Declaring int example 1
y = 50 // declaring int example 2, the compiler recognizes based on value and takes type and declaration
val z = "My test string" // creates z as a string
Semicolons at end of the line are not necessary unless you have multiple statements in the same line
val x: Int = 42 // works fine
var y: Int = 48 ; // Works fine
val a: String = "test"; val b: Int = 10; // Semicolon required after first stament but not recommanded to use the syntax
Data types in scala
Below are list of primitive data types and their declaration syntax
val str: String = "Hello"
val char: Char = 'c'
val i: Int = 10
val s: Short = 4612
val l: Long = 5394849949939392992L
val f: Float = 2.0453f
val d: Double = 3.23
Expressions in scala
In a statement(Instruction) the right hand side computation is called expressions
val a = 3 + 5 // The 3 + 5 part is a expression
The if in scalal is an expression, i.e. you can use it as expression and the final out can be used for different purposes
val test = if(str == "Hello") 3 else 5
val test = (b = "World") // The b = "World" is not returning anything, in scala it would return Unit which is something like void
Code block
A code block is a block of statements that assigned to variable
val test = {
val a = 10 // scop of a, and b variables are with in the block
val b = 5
if(a > b) "A is big" else "B is big"
}
In scala avoid using while loop as much as possible
Functions
In scala functioan would be created with def keyword. There are multiple combinations a function can created, below are few samples
object VariableTypes extends App {
def myDefination(param1: String, param2: Int): String = {
param1 + param2
}
def myNoParameterDef(): Int = 7
def recursiveDef(num: Int): String = {
if(num == 1) "The num "+num
else "The num "+num +"\n"+ recursiveDef(num-1)
}
println(recursiveDef(5))
def arthematicOperations(operator: String, num1: Int, num2: Int): Int = {
def add(num1: Int, num2: Int): Int = num1 + num2
def multiply(num1: Int, num2: Int): Int = num1 * num2
if(operator == "*") multiply(num1, num2) else add(num1, num2)
}
println(arthematicOperations("*", 2, 8))
// Call by value and default values
def callByVal(x: Lang = 10): Unit {
println("The x value is: "+ x);
}
// Call by Name(AKA call by reference)
def callByName(x: => Lang): Unit {
println("The x value is:" + x);
}
}
In functional programing recursive functions are recommanded over loops
Type inference
Scala can ercognize the return type based on code, this is called type inference.
default parameters
Scala supports default parameters, in scala it is possible to skip any default parameter by following special syntax
def operate(operator: String = "+", num1: Int = 10, num2: Int = 20): Int = {
if(operator == "*") num1 * num2 else num1 + num2
}
opeate() // This will do addition and return 30
operate("*") // The second two are default params and returns multiplcation 200
operate("+", 5) // The last parameter is default and returns 25
operate(num1=1) // the first and thrid parameters are default and returns 21
opearate(num2=3,num1=1) // The first param is default and returns 4, and order of params can be changed as we use names