Different Types of Operators in the Dart
The several types of operators in Dart, which we will discuss with the dart program, are as follows:
- Arithmetic Operators
- Relational Operators
- Type Test Operators
- Bitwise Operators
- Assignment Operators
- Logical Operators
- Cascade Notation Operator
How to Use the Operators in the Dart?
We have some simple examples for the demonstration of the dart built-in operators. These examples are as follows:
Example # 1: Using the Arithmetic Operators in a Dart
These are the operators that are utilized to implement arithmetic operations over operands. They are binary operators, which means they have two operands to act on.
We implement the arithmetic operations in the example above. Firstly, we have initialized two numbers in a variable, “x” and “y”. Then, we performed the arithmetic operations on these two numbers. Firstly, we have used the “+” operator and assigned it to a variable “sum”. After that, we have performed difference (-), product (*), division (/), another way of division(~/), and the modulus (%) operator, respectively. Each operator has a different result which will be printed with the print statement.
The arithmetic operator has the following results as shown:
Example # 2: Using the Relational Operators in a Dart
This class of operators is used over operands to perform relational operations
The above method includes the relational operators on the numbers, which are initialized in the variable “num1” and “num2”. On the given operands, we used the greater than (>), less than (), greater than equal to (>=), less than equal to (=), equal to (==), and not equal to (!=) operators. These operators are used in separate variables but on the same operands.
The results of relational operators are shown as follows.
Example # 3: Using the Type Test Operators in a Dart
This category of operators includes operators that compare operands.
In the above example program, we have created a string variable “val1” and a double variable “val2”. These variables are initialized with the values according to the data type set for these variables. Then, within our print function, we have used the “is” keyword in between the variable “val1” and the data type “String”. If the item has a certain type, the “is” operator returns true as a Boolean value. In the second print function, we have “is” with not equal to “!” operator. This operator is used when the object has a specified type; it returns the Boolean value false.
Thus, we have the type test operator output as a Boolean result as follows:
Example # 4: Using the Bitwise Operators in a Dart
This category of operators includes operators that perform bitwise operations on operands. Only individual integer numbers are utilized with bitwise operators. Their operation is similar to that of digital electronics logical gates (AND, OR, XOR, and so on) to execute various bit operations on integers. Communication across USB ports/sockets, data comparison, and encryption are real-world applications of bitwise operators. To alter data bit by bit, these operators work with binary values of 0 and 1.
Here, we have performed a bitwise operation while using bitwise operators on the given operands. These operands are stored in variables that are represented as “n1” and “n2” in the code. The bitwise operators include AND (&), OR(|), XOR(^), NOT(~), LEFT SHIFT(<<), and RIGHT SHIFT (>>). Each of these has a specific task such that the bitwise AND operator compares both operands via the Logical AND operation. The output bit is 1 if both bit’s values are 1; otherwise, it is 0. The | operator uses the Logical OR operation to compare its two operands. The generated bit is 1 if one of the bit values is 1; otherwise, it is 0.
The ^ operator uses the logical XOR operation to compare its two operands where the resultant bit will be 1; if both bits are different, else it will be 0. After that, we have the bitwise NOT operation where the operand’s bits are inverted. In the end, we have bitwise shift left and the bitwise shift right operators used. The bitwise shift left operator shifts n1 to the left by n2 bits and inserts 0 from the right in binary form. The bitwise right shift operator shifts n1 to n2 bits to the left and inserts a 0 from the left.
These are the following outputs that we got from the bitwise operators from the above example.
Example # 5: Using the Assignment Operators in a Dart
This type of operator includes operators that assign values to operands.
As we have created the variable for the operand initialization. Then, performed assignment operator operations on it. At first, we used the assignment operator to assign a value to variable “a” by using the equal to(=) assignment operator. After that, we have assigned a value to variable “d”, and variable “c” only as these are null.
The output of the assignment operators operation is as follows:
Example # 6: Using the Logical Operators in a Dart
This group of operators includes operators that are used to combine two or more operand conditions logically.
The logical operators are included in this example. Firstly, we have implied the logical AND operator, and its symbol is “&&”, which returns true if both conditions are true. Then, the logical OR operator(||) is used, which adds two conditions, and it will return true if one of them is true. The last operator we used in the above code is the logical NOT (!) operator, which is used to reverse the outcome.
The results of the logical operators are shown in the following feature image.
So, in the dart, there are a variety of operators. By providing built-in functionality, they make our operation much more convenient and smooth. With the example illustration, we took every point to include all of the primary dart operators. These operators are very easy to use once you understand them, and they will solve your complex calculation very conveniently.