Back to questions
JuniorTheoryCommon
What does the program print?
fun main() {
val max = Int.MAX_VALUE
println(max + 1)
}-2147483648
- Expecting
2147483648:max + 1adds twoIntvalues, so the result is anInttoo; the number does not fit, and it wraps around to-2147483648. - Expecting an exception: unlike division by zero, overflow throws nothing in Kotlin; the value silently wraps from the top of the range to the bottom.
- How do you change the program so that it prints
2147483648? - What does
Int.MIN_VALUE - 1give?