'Java Compound Operator

I have a loop for palindrome checking and I want to ask why does it work when i do

b=b*10+a 

but when I do

b*=10+a

it doesn't work even though it's short for b=b*10+a.

int a=0,b=0;

while (number>0) {
    a=number%10;
    b=b*10+a;
    number/=10;
}

int a=0,b=0;

while (number>0) {
    a=number%10;
    b*=10+a;
    number/=10;
}


Solution 1:[1]

Because those are two completely different operations.

Let's say b=5 and a=2, then

b=b*10+a

is going to set b = 52, ( (5*10) + 2)

b*=10+a

is going to set b = 60, (5 * (10+2))

Solution 2:[2]

b=b*10+a 

computes b*10 + a and assignes the result as the new value of b.

b*=10+a

is equivalent to

b = b * (10 + a)

so it computes b * (10 + a) and assigns the result as the new value of b.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ryan
Solution 2 Lajos Arpad