Difference in the practice of arithmetic and assignment operators

4
package entities;
public class Product {
      public String name;
      public double price;
      public int quantity;

      public double totalValueInStock() {
          return price * quantity;
      }
      public void addProducts(int quantity) {
          this.quantity += quantity;
      }
      public void removeProducts(int quantity) {
          this.quantity -= quantity;
      }
      public String toString() {
          return name
                + ", $ "
                + String.format("%.2f", price)
                + ", "
                + quantity
                + " units, Total: $ "
                + String.format("%.2f", totalValueInStock());
      }
}'

I thought I understood the difference between these basic topics but I could not explain the real difference between them ...

Note that: this.quantity += quantity; , the assignment operator + = was used that says "this.quantity RECEIVES this.quantity + quantity".

But I can not see the difference between it and the arithmetic operator +, they would both do the same operation, right? I could not understand this "RECEIVE".

What would be the real difference between one and the other in a clear way, without saying what example: a += 2 means "a RECEIVE a + 2".

This I understood, if "a" is worth 10 the result of a + = 2 is 12 however, a + 2 also results in 12. Did you understand me?

Why use one and not another?

    
asked by anonymous 11.12.2018 / 01:22

3 answers

5

The correct name of what you are using is composite operator. Because it is composed of an arithmetic plus an assignment of the result.

Depending on the implementation of the language its use can enable some optimization and consequent use of a more efficient instruction and therefore have a better performance (% with% X% with%).

When typing numeric types this form infers the correct type, in the separate arithmetic can give typing error.

In addition, it gives a clearer semantics to those who know how to program that is changing the state with that arithmetic operation, not to mention that it is shorter, and the short one tends to be more readable.

From a conceptual point of view they do the same thing and the compound operator can be read as a contraction of the expression doing the arithmetic and then the assignment.

But in terms of how it performs, contrary to what was said in another answer, it is not just taste, although it gives the same result.

    
11.12.2018 / 02:59
3

It's exactly the same, whether you can use one or the other, just preferences ! Some say it also plays a bit in code understanding (a = a + 2 is easier to understand than a + = 2) but do not worry too much about it.

    
11.12.2018 / 01:35
2

There is no conceptual difference between the two. In both cases, you are setting a to become the current value of a plus 2, which you correctly stated to be 12.

The term "receive" is common in programming and does not necessarily have to do with this compound operator += , but with any operation in which you assign a value to a variable. When it says " a gets a + 2 ", it's just a way of saying that this value is being "assigned", "set", "allocated" in variable a .

String nome = "José" can be read as " nome variable receives value" Jose ".

    
11.12.2018 / 10:00