Header Ads Widget

Basic Types and Operators

Basic Types and Operators

Basic Data Types:

Sr. No.Data TypeDescription
1

Byte

8 bit signed value. Range from -128 to 127

2Short16 bit signed value. Range -32768 to 32767
3Int32 bit signed value. Range -2147483648 to 2147483647
4Long64 bit signed value. -9223372036854775808 to 9223372036854775807
5Float32 bit IEEE 754 single-precision float
6Double64 bit IEEE 754 double-precision float
7Char16 bit unsigned Unicode character. Range from U+0000 to U+FFFF
8StringA sequence of Chars
9BooleanEither the literal true or the literal false
10UnitCorresponds to no value
11Nullnull or empty reference
12NothingThe subtype of every other type; includes no values
13AnyThe supertype of any type; any object is of type Any
14AnyRefThe 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.

OperatorDescription
+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

OperatorDescription
==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.

OperatorDescription
&&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 −

pqp & qp | qp ^ q
00000
01011
11110
10011
OperatorDescription
&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 −

OperatorDescription
=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

Post a Comment

0 Comments