0 votes
in Kotlin by
Explain Operators in Kotlin

1 Answer

0 votes
by

Operators are momentous and special characters or symbols that assures the operations in operands that have variables and values. In Kotlin, there is a set of operators that are used in different operations like arithmetic operations, assignment operations, comparison operations, etc.

Arithmetic Operators :

Arithmetic operators are used for addition, subtraction, multiplication, division, and modulus.

OperatorMeaning
+Addition (also used for string concatenation)
Subtraction Operator
*Multiplication Operator
/Division Operator
%Modulus Operator

For Example:

var x = 10
var y = 20
var z = ( ( x + y ) * ( x + y ) ) / 2     

Here the output of the following code is 45.

Comparison Operators :

Comparison operator is used to compare two values, two variables or two numbers. It is used with the greater than symbol( > ), less than symbol( < ) and equal to symbol( ==), not equal to symbol( != ), greater than equal to symbol( >= ), less than equal to symbol(<= ). It always results in true or false.

For Example:

var x = 10
Var y =20
Var z = x < y // Output : true
Var w = x > y // Output : false
Var m = x == y // Output : false

Assignment Operators :

Assignment Operators are used to assign the arithmetic operated values. It is used with the symbols like +=, -=, *=, /=, %=.

For Example:

var x = 10
var y = 20
var x + = y // Output : 30
Var y - = x // Output : 10
Var x * = y // Output : 200

Increment and Decrement Operators :

Increment and decrement operators are used to increment and decrement the values of the variable or number. It is used with the help of symbols like ++ and —.

There are two types of increment and decrement that are pre-increment ++a, post-increment a++, pre decrement –b, post decrement b–.

For Example:

var a = 10
var b = 20
var c = a++ // Output: 11
var d = b— //Output : 19    
...