Home > Software design >  Java Compound Operator
Java Compound Operator

Time:03-23

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;
    b=b*10 a;
    number/=10;
}

int a=0,b=0;

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

CodePudding user response:

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))

CodePudding user response:

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.

  • Related