In this tutorial, you shall learn about Addition operator in Kotlin, its syntax, and how to use Addition operator in programs, with examples.

Kotlin Addition

Kotlin Addition Operator takes two operands as inputs and returns their sum.

+ symbol is used for Addition Operator.

Syntax

The syntax for Addition Operator is

operand_1 + operand_2

The operands could be of any numeric datatype.

If the two operands are of different datatypes, implicit datatype promotion takes place and value of lower datatype is promoted to higher datatype.

ADVERTISEMENT

Examples

1. Add two integer numbers

In the following example, we shall add two integers using Addition Operator.

Main.kt

fun main() {
    var a = 10
    var b = 20
    var sum = a + b
    println("Sum : $sum")
}

Output

Sum : 30

2. Add values of different types

In the following example, we shall add an integer and a Float number using Addition Operator.

As we are adding values of two different datatypes, one with lower datatype promotes to higher datatype and the result is of higher datatype. In the following example, Int would be promoted to Float, and the result would be Float.

Main.kt

fun main() {
    var a: Int = 10
    var b: Double = 3.14
    var sum = a + b
    println("Sum : $sum")
}

Output

Sum : 13.14

Conclusion

In this Kotlin Tutorial, we learned how to use Addition Operator to add two numbers.