LEARN SCALA IN ONE DAY
In this post we’ll teach the Scala programming language on below topics and you will get good amount of knowledge of Scala Programming in a day which you can use in Spark with Scala context.
We will cover installation Scala on Windows.We’ll also look at using the REPL versus compiling code. We’ll also cover Data Types, Math Functions, If, Main, While, Do While, For, receiving user input, Output Styling, Strings, Functions, Recursion, Arrays, Array Buffers, Yield, For Each, Maps, Tuples, Classes, Companion Objects, Inheritance, Abstract Classes, Traits, Higher Order Functions, Map, Filter, File I/O, Exception Handling and much more.
Scala Overview.
Scala, short for Scalable Language, is a hybrid functional programming language. It was created by Martin Odersky. Scala smoothly integrates the features of object-oriented and functional languages. Scala is compiled to run on the Java Virtual Machine. Many existing companies, who depend on Java for business critical applications, are turning to Scala to boost their development productivity, applications scalability and overall reliability.
Here we have presented a few points that makes Scala the first choice of application developers.
1. Scala is Object-Oriented
Scala is a pure Object-Oriented Language which means that every value is an object.
2. Scala is functional
Scala is also a functional language in the sense that every function is a value and every value is an object so ultimately every function is an object.
3. Scala runs on the JVM
Scala is compiled into Java Byte Code which is executed by the Java Virtual Machine (JVM). This means that Scala and Java have a common runtime platform. You can easily move from Java to Scala.
4. Scala can Execute Java Code
Scala enables you to use all the classes of the Java SDK and also your own custom Java classes, or your favorite Java open source projects.
5. Scala can do Concurrent & Synchronize processing
Scala allows you to express general programming patterns in an effective way. It reduces the number of lines and helps the programmer to code in a type-safe way. It allows you to write codes in an immutable manner, which makes it easy to apply concurrency and parallelism (Synchronize).
6. Scala vs Java
Scala has a set of features that completely differ from Java. Some of these are
- All types are objects
- Type inference
- Nested Functions
- Functions are objects
- Domain specific language (DSL) support
- Traits
- Closures
- Concurrency support inspired by Erlang
Scala Installation
The most useful command in Scala
| Description | Command / Script | Comments |
| To open Scala in command prompt | scala | Type scala from comand prompt and hit Enter |
| To print the text | println | Example: println("Hello, Scala!"); |
| Comments in Scala | // (For Single Line)
/* */ (For Multiline) | Example: // This is my First Scala Program
Example: /* This is my first java program.
This will print 'Hello World' as the output */ |
| Compile the scala program | scalac | ‘scalac’ command is used to compile the Scala program and it will generate a few class files in the current directory.Example: scalac Helloworld.scala |
| Execute the scala program. | scala | Example: scala Helloworld |
| Quit from Scala | :q | Example: Scala > q: <Enter> |
| To Clear the screen in Scala | cls | Type cls and press enter |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
Basic Syntax and coding conventions in Scala programming
| Sr No | Basic Syntax | Syntax Descriptions |
| 1 | Case Sensitivity | Scala is case-sensitive, which means identifier Hello and hello would have different meaning in Scala. |
| 2 | Class Names |
For all class names, the first letter should be in Upper Case. If several words are used to form a name of the class, each inner word's first letter should be in Upper Case.
Example: class MyFirstScalaClass. |
| 3 | Method Names |
All method names should start with a Lower Case letter. If multiple words are used to form the name of the method, then each inner word's first letter should be in Upper Case.
Example: def myMethodName() |
| 4 | Program File Name |
Name of the program file should exactly match the object name. When saving the file you should save it using the object name (Remember Scala is case-sensitive) and append ‘.scala’ to the end of the name. (If the file name and the object name do not match your program will not compile).
Example: Assume 'HelloWorld' is the object name. Then the file should be saved as 'HelloWorld.scala'.
|
| 5 | def main(args: Array[String]) | Scala program processing starts from the main() method which is a mandatory part of every Scala Program. |
| 6 | To be added ... | |
| 7 | To be added ... | |
| 8 | To be added ... | |
| 9 | To be added ... | |
| 10 | To be added ... | |
Data Types in Scala
| Sr No | Data Type | Descriptions |
| 1 | Byte | 8 bit signed value. Range from -128 to 127 |
| 2 | Short | 16 bit signed value. Range -32768 to 32767 |
| 3 | Int | 32 bit signed value. Range -2147483648 to 2147483647 |
| 4 | Long | 64 bit signed value. -9223372036854775808 to 9223372036854775807 |
| 5 | Float | 32 bit IEEE 754 single-precision float |
| 6 | Double | 64 bit IEEE 754 double-precision float |
| 7 | Char | 16 bit unsigned Unicode character. Range from U+0000 to U+FFFF |
| 8 | String | A sequence of Chars |
| 9 | Boolean | Either the literal true or the literal false |
| 10 | Unit | Corresponds to no value |
| 11 | Null | null or empty reference |
| 12 | Any | The supertype of any type; any object is of type Any |
| 13 | AnyRef | The supertype of any reference type |
| 14 | | |
| 15 | | |
Operators in Scala.
| Operator | Description | Example |
| 1. Arithmetic Operators in Scala: For example, let us assume variable A holds 10 and variable B holds 20, then |
| + | Adds two operands | A + B will give 30 |
| - | Subtracts second operand from the first | A - B will give -10 |
| * | Multiplies both operands | A * B will give 200 |
| / | Divides numerator by de-numerator | B / A will give 2 |
| % | Modulus operator finds the remainder after division of one number by another | B % |
| 2. Relational Operators in Scala : For example let us assume variable A holds 10 and variable B holds 20, then |
| == | Checks if the values of two operands are equal or not, if yes then condition becomes true. | (A == B) is not true. |
| != | Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. | (A != B) is true. |
| > | Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. | (A > B) is not true. |
| < | Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. | (A < B) is true. |
| >= | Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. | (A >= B) is not true. |
| <= | Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. | (A <= B) is true. |
| 3. Logical Operators in Scala : For example, assume variable A holds 1 and variable B holds 0, then |
| && | It is called Logical AND operator. If both the operands are non zero then condition becomes true. | (A && B) is false. |
| || | It is called Logical OR Operator. If any of the two operands is non zero then condition becomes true. | (A || B) is true. |
| ! | It is called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. | !(A && B) is true. |
| 4. Assignment Operators in Scala |
| = | Simple assignment operator, Assigns values from right side operands to left side operand | C = A + B will assign value of A + B into C |
| += | Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand | C += A is equivalent to C = C + A |
| -= | Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand | C -= A is equivalent to C = C - A |
| *= | Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand | C *= A is equivalent to C = C * A |
| /= | Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand | C /= A is equivalent to C = C / A |
| %= | Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand | C %= A is equivalent to C = C % A |
| <<= | Left shift AND assignment operator | C <<= 2 is same as C = C << 2 |
| >>= | Right shift AND assignment operator | C >>= 2 is same as C = C >> 2 |
| &= | Bitwise AND assignment operator | C &= 2 is same as C = C & 2 |
| ^= | bitwise exclusive OR and assignment operator | C ^= 2 is same as C = C ^ 2 |
| |= | bitwise inclusive OR and assignment operator | C |= 2 is same as C = C | 2 |
Mathematical Functions in Scala
IF-ELSE conditions in Scala
WHILE and Do While in Scala
FOR LOOP in Scala
Receiving User Input in Scala
Functions in Scala
0 comments:
Post a Comment