Basic Types and Operators
Basic Data Types:
Sr. No. | Data Type | Description |
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 | Nothing | The subtype of every other type; includes no values |
13 | Any | The supertype of any type; any object is of type Any |
14 | AnyRef | The supertype of any reference type |
Operators:
An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. Scala is rich in built-in operators and provides the following types of operators −
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
→ Arithmetic Operators
The following arithmetic operators are supported by the Scala language.
Operator | Description |
+ | Adds two operands |
- | Subtracts second operand from the first |
* | Multiplies both operands |
/ | Divides numerator by de-numerator |
% | Modulus operator finds the remainder after division of one number by another |
→ Relational Operators
The following relational operators are supported by the Scala language
Operator | Description |
== | Checks if the values of two operands are equal or not, if yes then the condition becomes true. |
!= | Checks if the values of two operands are equal or not, if values are not equal then the condition becomes true. |
> | Checks if the value of the left operand is greater than the value of the right operand, if yes then the condition becomes true. |
< | Checks if the value of the left operand is less than the value of the right operand, if yes then the condition becomes true. |
>= | Checks if the value of the left operand is greater than or equal to the value of the right operand, if yes then the condition becomes true. |
<= | Checks if the value of the left operand is less than or equal to the value of the right operand, if yes then the condition becomes true. |
→ Logical Operators
The following logical operators are supported by the Scala language.
Operator | Description |
&& | It is called Logical AND operator. If both the operands are non zero then the condition becomes true. |
|| | It is called Logical OR Operator. If any of the two operands is non zero then the condition becomes true. |
! | It is called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then the Logical NOT operator will make it false. |
→ Bitwise Operators
Bitwise operator works on bits and performs bit by bit operation. The truth tables for &, |, and ^ are as follows −
p | q | p & q | p | q | p ^ q |
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
1 | 0 | 0 | 1 | 1 |
Operator | Description |
& | Binary AND Operator copies a bit to the result if it exists in both operands. |
| | Binary OR Operator copies a bit if it exists in either operand. |
^ | Binary XOR Operator copies the bit if it is set in one operand but not both. |
~ | Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. |
<< | Binary Left Shift Operator. The bit positions of the value of the left operand are moved left by the number of bits specified by the right operand. |
>> | Binary Right Shift Operator. The bit positions of the left operand value are moved right by the number of bits specified by the right operand. |
>>> | Shift right zero-fill operator. The left operands value is moved right by the number of bits specified by the right operand and shifted values are filled up with zeros. |
Assignment Operators
There are the following assignment operators supported by Scala language −
Operator | Description |
= | Simple assignment operator, Assigns values from right side operands to left side operand |
+= | Add AND assignment operator, It adds the right operand to the left operand and assigns the result to the left operand |
-= | Subtract AND assignment operator, It subtracts right operand from the left operand and assigns the result to left operand |
*= | Multiply AND assignment operator, It multiplies right operand with the left operand and assigns the result to the left operand |
/= | Divide AND assignment operator, It divides left operand with the right operand and assigns the result to left operand |
%= | Modulus AND assignment operator, It takes modulus using two operands and assign the result to the left operand |
<<= | Left shift AND assignment operator |
>>= | Right shift AND assignment operator |
&= | Bitwise AND assignment operator |
^= | bitwise exclusive OR and assignment operator |
|= | bitwise inclusive OR and assignment operator |
0 Comments