Home Cheatsheet Scala Cheatsheet

Scala Cheatsheet

by anupmaurya
58 minutes read

Scala (/ˈskɑːlɑː/ SKAH-lah) is a strong statically typed general-purpose programming language which supports both object-oriented programming and functional programming. Designed to be concise, many of Scala’s design decisions are aimed to address criticisms of Java.

Sample program in Scala

object HelloWorld {
	def main(args: Array[String]): Unit = {
	println("Hello, World!")
	}
}
  • object : Object is a instance of a class. In Scala, everything is considered as objects.
  • def : def is used for function declaration
  • main : entry point of the program
  • println : prints data to the console.
  • // : Single line comment
  • /* Multi
    Line
    comment */

Data types in Scala

Data typeDescriptionRangeSize
intused to store whole numbers-2,147,483,648 to 2,147,483,6474 bytes
shortused to store whole numbers-32,768 to 32,7672 bytes
longused to store whole numbers-9,223,372,036,854,775,808 to 9,223,372,036,854,775,8078 bytes
byteused to store whole numbers-128 to 1271 byte
floatused to store fractional numbers6 to 7 decimal digits4 bytes
doubleused to store fractional numbers15 decimal digits8 bytes
booleancan either store true or falseeither true or false1 bit
charused to store a single characterone character2 bytes
stringused to store stringssequence of characters2bytes per character

Variables in Scala

var varName = value
val  varName = value
varName = value
var varName : DataType = value

Operators in Scala

TypeOperators
Arithmetic Operators+ , – , * , / , %
Comparision Operators== , != , > , >= , < , <=
Bitwise Operators& , ^ , | , ^ , ~ , << , >> , >>>
Logical Operators&& , || , !
Assignment Operators= , += , -= , *= , /= , %= , &= , ^= , |= , <<= , >>=

Conditional Statements

1. If

if(conditional-expression)
{
    //code
}

2. If-else

if(conditional-expression)
{
    //code
} else {
    //code
}

3. If-else-if ladder

if(conditional-expression-1)
{
    //code
} else if(conditional-expression-2) {
    //code
} else if(conditional-expression-3) {
    //code
}
....
else {
    //code
}

Loops

1. For

for(var <- range){  
//code  
} 

2. For loop Using until

for( var <- initialValue until maxValue ){  
    //code  
}

3. For loop filtering

for( var <- initialValue until maxValue if condition){  
  //code
}

4. For loop for collections like lists, sequence etc

for( varName <- Collections){             
  //code
}

5. While

while(condition){  
//code 
}  

6. Do-while

do{  
//code 
}while(condition); 

Arrays in Scala

Array is a collection of similar data which is stored in continuous memory addresses.

Declaring 1-D array

var arrayName:Array[datatype] = new Array[datatype](size)

//or

var arrayName = new Array[datatype](size)

Declaring 2-D array

var arrName = ofDim[datatype](rowsize,colSize)

Example

 var arr1 = Array(1, 2, 3, 4, 5)

Functions in Scala

How to declare a Function

def functionName ([argumentsList]) : [return type]

How to define a Function

def functionName ([argumentsList]) : [return type] [=] {
   // code
   return [expr]
}

How to call a Function

function_name (argumentsList)

If you have quires and suggestions put them in the comments.

You may also like

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.