English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this program, you will learn how to store and add two integers in Kotlin. After the addition, the final sum will be displayed on the screen.
fun main(args: Array<String>) { val first: Int = 10 val second: Int = 20 val sum = first + second println("The sum is: $sum") }
When the program is run, the output is:
The sum is: 30
In this scheme, similar to Java, two integers10and20 is stored in integer variables first and second respectively. It is not necessary to add :Int variable declaration, Kotlin will automatically assign the type (in this case, Int).
Then, use + The operator adds first and second and stores the result in another variable sum.
Finally, use the println() function to print the sum on the screen.
This is the equivalent code in Java: In JavaAdd Two Integers.