Scala Cheatsheet

by anupmaurya
572 views

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

object HelloWorld { def main(args: Array[String]): Unit = { println("Hello, World!") } }
Code language: JavaScript (javascript)
  • 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
Code language: JavaScript (javascript)

Operators in Scala

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

Conditional Statements

1. If

if(conditional-expression) { //code }
Code language: JavaScript (javascript)

2. If-else

if(conditional-expression) { //code } else { //code }
Code language: JavaScript (javascript)

3. If-else-if ladder

if(conditional-expression-1) { //code } else if(conditional-expression-2) { //code } else if(conditional-expression-3) { //code } .... else { //code }
Code language: PHP (php)

Loops

1. For

for(var <- range){ //code }
Code language: JavaScript (javascript)

2. For loop Using until

for( var <- initialValue until maxValue ){ //code }
Code language: JavaScript (javascript)

3. For loop filtering

for( var <- initialValue until maxValue if condition){ //code }
Code language: JavaScript (javascript)

4. For loop for collections like lists, sequence etc

for( varName <- Collections){ //code }
Code language: JavaScript (javascript)

5. While

while(condition){ //code }
Code language: JavaScript (javascript)

6. Do-while

do{ //code }while(condition);
Code language: JavaScript (javascript)

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)
Code language: JavaScript (javascript)

Declaring 2-D array

var arrName = ofDim[datatype](rowsize,colSize)
Code language: JavaScript (javascript)

Example

var arr1 = Array(1, 2, 3, 4, 5)
Code language: JavaScript (javascript)

Functions in Scala

How to declare a Function

def functionName ([argumentsList]) : [return type]
Code language: CSS (css)

How to define a Function

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

How to call a Function

function_name (argumentsList)

If you have quires and suggestions put them in the comments.Hope this Scala Cheatsheet useful to you, Share & Help Other to learn 🙂

You may also like