What is the difference between -= and =-?

Asked

Viewed 1,171 times

14

In java and other languages I’ve seen in some projects things like:

saldo -= 100 and sometimes saldo =- 100 but I could never understand the difference between those two guys, if there’s any difference.

After all, there is some difference between performing an operation with -= and =- ?

  • 1

    Are you sure you’ve seen saldo =- 100 somewhere? You’re basically assigning -100 to the variable. I don’t see this as possible considering the same context of saldo -= 100.

  • 2

    Not to mention that the doubt is the same, the answers answer to both cases.

  • @Article thank you!

  • @Andersoncarloswoss, where does the example given interfere with the answer? I believe it did not affect the understanding, so much so that the staff have already given some concrete answers.

  • Yes, that was the question. Anyway, thanks for the comments.

  • reply here https://stackoverflow.com/questions/8710619/why-dont-javas-compound-assignment-operators-require-casting

Show 1 more comment

4 answers

22


saldo -= 100 is the same thing as saldo = saldo - 100.

saldo =- 100 is assigning -100 to the variable saldo. It’s clearer to visualize like this:

saldo = -100

  • Thank you for the reply.

9

Hello! I know that your doubt has been solved by friends. I will only share one more explanation about it, as it is the one I usually pass when someone asks me the same question. Then goes as "a complement" to the above answers.

Behold:

saldo = 500;
saldo -= 100;

System.out.println(saldo);

Output window prints:

run:
400

In this next logic, the first assignment in the value of 500, is no longer valid for variable balance and the account owner still had negative balance.

saldo = 500;
saldo =- 100;

System.out.println(saldo);

Output window prints:

run:
-100

Remembering that the account owner will also have negative balance, if done so:

saldo = 500;
saldo -= 600;

System.out.println(saldo);

Output window prints:

run:
-100
  • Thank you, @Carlosheuberger! Remembering that the run:... , is not part of the code. It is the part that script prints to the Output window (along with other compiler script results).

9

The operator -= java is used when we want to assign the value of a variable to subtract a second value from the current value of this variable.

Take this example:

int a -= b;

is the same as doing:

int a = a - b;

Already =- is not an operator, it is simply an assignment giving a negative signal to the value to be assigned to the right.

  • Thank you for the reply.

0

I think you understand the concept of increment and decrement wrong, see, in the first case you are actually decreasing the balance variable by -100, however; in the second case you are assigning the variable the value -100.

See the example below of a decrease:

class Teste {
    public static void main(String[] args) {
        int saldo = 14000;
        int i = 0;
        for(i=0; i < 5; i++) {
            saldo -= 100; //Aqui tem um decremento
            System.out.println("saldo : "+ saldo);
        }
    }
}

Its result is:

saldo : 13900
saldo : 13800
saldo : 13700
saldo : 13600
saldo : 13500

Now notice if you do the reverse, which is to put the sign - after the =assignment signal, getting sando =- 100

class Teste {
    public static void main(String[] args) {
        int saldo = 14000;
        int i = 0;
        for(i=0; i < 5; i++) {
            saldo =- 100; //Aqui tem uma atribuição, isto é, mesma coisa que saldo = -100;
            System.out.println("saldo : "+ saldo);
        }
    }
}

Result, was -100 in everything, because each loop the variable balance was assigned the value of -100:

saldo : -100
saldo : -100
saldo : -100
saldo : -100
saldo : -100

Browser other questions tagged

You are not signed in. Login or sign up in order to post.