Scala – Data types and operators

  • date 30th September, 2020 |
  • by Prwatech |
  • 0 Comments

Guide to Data Types in Scala

Data Types

1. Byte: It is an 8 bit signed value.

2. Short: It is a 16 bit signed value.

3. Int: It is a 32 bit signed value.

4. Long: It is a 64 bit signed value.

5. Float: It can store 32 bit of data

6. Double: It can store 64 bit of data

7. Char: It is a 16 bit unsigned Unicode character.

8. String: It is a sequence of Chars

9. Boolean: It holds either of these values the literal true or the literal false

10. Null: It holds empty reference or null

12. Nothing: It is the subtype of every other data type which includes no values

13. Any: It is the super type of any type where any object is of type Any

14. AnyRef: It is the super type of any reference type No.

Operators

An operator is a symbol that instructs the compiler to perform any mathematical or logical manipulations. Scala is rich in built-in operators and different types of operators are:

  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Bitwise Operators
  5. Assignment Operators

Arithmetic Operators

The following are the arithmetic operators supported by Scala language.

Operator Description
+ Adds two operands
Subtracts second operand from the first
* Multiplies both the operands
/ Divides numerator by de-numerator
% Modulus operator finds the remainder after division of one number by another

For example, if variables A=6 and B=16, then

Relational Operators

The following are the relational operators supported by Scala language.

Operator Description
== Checks if the values of two operands are equal or not, if yes then condition becomes true else False.
!= Checks if the values of two operands are equal or not, if values are not equal then condition becomes true else False.
Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.
Checks if the value of left operand is less than the value of right operand, if yes then condition becomes 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.
<= Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.

For example, if variables A=6 and B=16, then

 

Logical Operators

The following logical operators are supported by Scala language.

Operator Description
&& It is called Logical AND operator. If both the operands are non-zero then condition becomes true.
|| It is called Logical OR Operator. If any of the two operands is non zero then condition becomes 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.

For example, if variables b1=true and b2=false, then

Prog1. Define 1 to natural numbers and sum them.

Prog2. Define 50 numbers which should start from 0 and find out only odd numbers. Then multiply all odd numbers with 2 and sum all of them.

0
0

Quick Support

image image