Basics

Kotlin Operators

Kotlin Operators

Kotlin operators include arithmetic and Elvis operator for nulls.

Introduction to Kotlin Operators

Kotlin, like many other programming languages, provides a range of operators that allow you to perform various operations on variables and values. Understanding these operators is essential for writing concise and efficient code.

Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, and division. Kotlin supports the following arithmetic operators:

  • + : Addition
  • - : Subtraction
  • * : Multiplication
  • / : Division
  • % : Modulus (remainder of division)

Comparison Operators

Comparison operators are used to compare two values. They return a Boolean result based on the comparison. The main comparison operators in Kotlin are:

  • == : Equal to
  • != : Not equal to
  • > : Greater than
  • < : Less than
  • >= : Greater than or equal to
  • <= : Less than or equal to

Logical Operators

Logical operators are used to combine multiple Boolean expressions or values. Kotlin provides the following logical operators:

  • && : Logical AND
  • || : Logical OR
  • ! : Logical NOT

The Elvis Operator

The Elvis operator (?:) is a unique feature in Kotlin that is used to handle nullability. It returns the left-hand operand if it is not null, otherwise it returns the right-hand operand. This operator is particularly useful for providing default values.

Conclusion

Kotlin operators are powerful tools that help in performing a variety of tasks from basic arithmetic to handling complex logical conditions. Understanding how to use these operators effectively can greatly enhance your Kotlin programming skills. In the next topic, we will dive into the use of conditional statements with 'If Else'.

Previous
Null Safety