Sobes.tech
Intern

Milleks on Dart keeles operaatorid vajalikud?

sobes.tech AI

Vastus AI-lt

Operators in Dart allow manipulating values and variables by performing various operations. These include arithmetic, conditional, logical, and other operators.

Main categories of operators:

  • Arithmetic: +, -, *, /, %
  • Equality and comparison operators: ==, !=, >, <, >=, <=
  • Assignment operators: =, +=, -=, *=, /=
  • Logical: &&, ||, !
  • Bitwise operations: &, |, ^, ~, <<, >>
  • Special operators:
    • .. (cascade operator)
    • ?. (conditional member access)
    • ?? (null check operator)
    • is (type check)
    • as (type cast)

Example of usage:

int a = 10;
int b = 5;

// Arithmetic operator
int sum = a + b; // 15

// Equality operator
bool isEqual = (a == b); // false

// Logical operator
bool condition = (a > 5 && b < 10); // true

// Assignment with operation operator
a += 2; // a now 12

// Cascade operator
var buffer = StringBuffer()
  ..write('hello')
  ..write('world'); // buffer contains "helloworld"

// Null check operator
String? name = null;
String displayName = name ?? 'Guest'; // displayName will be 'Guest'