0

Here's the problem in which I encountered this issue:

The function should compare the value at each index position and score a point if the value for that position is higher. No point if they are the same. Given a = [1, 1, 1] b = [1, 0, 0] output should be [2, 0]

fun compareArrays(a: Array<Int>, b: Array<Int>): Array<Int> {

    var aRetVal:Int = 0
    var bRetVal:Int = 0

    for(i in 0..2){
        when {
            a[i] > b[i] -> aRetVal + 1 // This does not add 1 to the variable
            b[i] > a[i] -> bRetVal++ // This does...
        }
    }
    return arrayOf(aRetVal, bRetVal)

}

The IDE even says that aRetVal is unmodified and should be declared as a val

1

4 Answers 4

7

What others said is true, but in Kotlin there's more. ++ is just syntactic sugar and under the hood it will call inc() on that variable. The same applies to --, which causes dec() to be invoked (see documentation). In other words a++ is equivalent to a.inc() (for Int or other primitive types that gets optimised by the compiler and increment happens without any method call) followed by a reassignment of a to the incremented value.

As a bonus, consider the following code:

fun main() {
    var i = 0
    val x = when {
        i < 5 -> i++
        else -> -1
    }

    println(x) // prints 0
    println(i) // prints 1

    val y = when {
        i < 5 -> ++i
        else -> -1
    }

    println(y) // prints 2
    println(i) // prints 2 
}

The explanation for that comes from the documentation I linked above:

The compiler performs the following steps for resolution of an operator in the postfix form, e.g. a++:

  • Store the initial value of a to a temporary storage a0;
  • Assign the result of a.inc() to a;
  • Return a0 as a result of the expression.

...

For the prefix forms ++a and --a resolution works the same way, and the effect is:

  • Assign the result of a.inc() to a;
  • Return the new value of a as a result of the expression.
Sign up to request clarification or add additional context in comments.

2 Comments

For a++, I get the new value assigned to a. For a.inc I don't get the new value assigned to a. In fact I can do a.inc(), also when a is a 'val' (immutable). So I would suggest that a++ and a.inc() is not equivalent.
@TorbjörnÖsterdahl that's right. What I meant is that a++ calls a.inc() under the hood, but it's true that the former implies the resulting value is reassigned to a in the end. I'll edit my answer to make it more clear
3

Because

variable++ is shortcut for variable = variable + 1 (i.e. with assignment)

and

variable + 1 is "shortcut" for variable + 1 (i.e. without assignment, and actually not a shortcut at all).

Comments

2

That is because what notation a++ does is actually a=a+1, not just a+1. As you can see, a+1 will return a value that is bigger by one than a, but not overwrite a itself.

Hope this helps. Cheers!

Comments

2

The equivalent to a++ is a = a + 1, you have to do a reassignment which the inc operator does as well.

This is not related to Kotlin but a thing you'll find in pretty much any other language

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.