Scala Tutorial
Total Page:16
File Type:pdf, Size:1020Kb
Scala Tutorial SCALA TUTORIAL Simply Easy Learning by tutorialspoint.com tutorialspoint.com i ABOUT THE TUTORIAL Scala Tutorial Scala is a modern multi-paradigm programming language designed to express common programming patterns in a concise, elegant, and type-safe way. Scala has been created by Martin Odersky and he released the first version in 2003. Scala smoothly integrates features of object-oriented and functional languages. This tutorial gives a great understanding on Scala. Audience This tutorial has been prepared for the beginners to help them understand programming Language Scala in simple and easy steps. After completing this tutorial, you will find yourself at a moderate level of expertise in using Scala from where you can take yourself to next levels. Prerequisites Scala Programming is based on Java, so if you are aware of Java syntax, then it's pretty easy to learn Scala. Further if you do not have expertise in Java but you know any other programming language like C, C++ or Python, then it will also help in grasping Scala concepts very quickly. Copyright & Disclaimer Notice All the content and graphics on this tutorial are the property of tutorialspoint.com. Any content from tutorialspoint.com or this tutorial may not be redistributed or reproduced in any way, shape, or form without the written permission of tutorialspoint.com. Failure to do so is a violation of copyright laws. This tutorial may contain inaccuracies or errors and tutorialspoint provides no guarantee regarding the accuracy of the site or its contents including this tutorial. If you discover that the tutorialspoint.com site or this tutorial content contains some errors, please contact us at [email protected] TUTORIALS POINT Simply Easy Learning Table of Content Scala Tutorial ........................................................................... 2 Audience .................................................................................. 2 Prerequisites ............................................................................ 2 Copyright & Disclaimer Notice .................................................. 2 Scala Overview ........................................................................ 8 Scala is object-oriented: ............................................................................ 8 Scala is functional: ..................................................................................... 8 Scala is statically typed: ............................................................................ 8 Scala runs on the JVM: ............................................................................. 8 Scala can Execute Java Code: .................................................................. 9 Scala vs Java: ........................................................................................... 9 Scala Web Frameworks: ........................................................................... 9 Scala Environment Setup ....................................................... 10 Installing Scala on Windows: ................................................................... 10 STEP (1): JAVA SETUP: ......................................................................... 10 STEP (2): SCALA SETUP: ..................................................................... 10 Installing Scala on Mac OS X and Linux .................................................. 11 STEP (1): JAVA SETUP: ........................................................................ 11 STEP (2): SCALA SETUP: ..................................................................... 11 Scala Basic Syntax ................................................................ 13 First Scala Program: ................................................................................ 13 INTERACTIVE MODE PROGRAMMING: ............................................... 13 SCRIPT MODE PROGRAMMING: .......................................................... 13 Basic Syntax: ........................................................................................... 14 Scala Identifiers: ...................................................................................... 15 ALPHANUMERIC IDENTIFIERS ............................................................. 15 OPERATOR IDENTIFIERS ..................................................................... 15 MIXED IDENTIFIERS .............................................................................. 15 LITERAL IDENTIFIERS ........................................................................... 15 Scala Keywords: ...................................................................................... 15 Comments in Scala ................................................................................. 16 Blank Lines and Whitespace: .................................................................. 16 Newline Characters: ................................................................................ 16 Scala Packages: ...................................................................................... 16 Scala Data Types ................................................................... 18 Scala Basic Literals: ................................................................................ 18 INTEGER LITERALS .............................................................................. 19 TUTORIALS POINT Simply Easy Learning FLOATING POINT LITERALS ................................................................ 19 BOOLEAN LITERALS............................................................................. 19 SYMBOL LITERALS ............................................................................... 19 CHARACTER LITERALS........................................................................ 19 STRING LITERALS ................................................................................ 19 MULTI-LINE STRINGS ........................................................................... 19 THE NULL VALUE .................................................................................. 20 ESCAPE SEQUENCES: ......................................................................... 20 Scala Variables ...................................................................... 21 Variable Declaration ................................................................................ 21 Variable Data Types: ............................................................................... 21 Variable Type Inference: ......................................................................... 22 Multiple assignments: .............................................................................. 22 Variable Types: ........................................................................................ 22 FIELDS: ................................................................................................... 22 METHOD PARAMETERS: ...................................................................... 22 LOCAL VARIABLES: ............................................................................... 22 Scala Access Modifiers .......................................................... 23 Private members: .................................................................................... 23 Protected members: ................................................................................ 23 Public members: ...................................................................................... 24 Scope of protection: ................................................................................. 24 Scala Operators ..................................................................... 25 Arithmetic Operators: ............................................................................... 25 Example: ................................................................................................. 25 Relational Operators: ............................................................................... 26 Example: ................................................................................................. 27 Logical Operators: ................................................................................... 27 Example: ................................................................................................. 27 Bitwise Operators: ................................................................................... 28 Example .................................................................................................. 29 Assignment Operators: ............................................................................ 30 Example: ................................................................................................. 30 Operator Precedence in Scala: ................................................................ 31 Scala IF…ELSE Statements .................................................. 33 The if Statement: ..................................................................................... 33 SYNTAX: ................................................................................................. 33 EXAMPLE:............................................................................................... 34 The if...else Statement: ............................................................................ 34 TUTORIALS POINT Simply Easy Learning SYNTAX: ................................................................................................. 34 EXAMPLE: .............................................................................................