Difference in arithmetic and assignment operators practice

Asked

Viewed 81 times

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 couldn’t explain the real difference between one and the other...

Look over there: this.quantity += quantity;, was used the += assignment operator that says "this.Quantity GETS this.Quantity + Quantity".

But I can’t see the difference between it and the arithmetic operator +, both would do the same operation, right? I couldn’t understand this "RECEIVES".

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

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

Why use one and not another?

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.

3 answers

5

The correct name of what you are using is compound operator. Because it consists 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 better performance (iadd X iinc).

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

It also gives a clearer semantics to those who know how to program who are changing the state with that arithmetic operation, not to mention that it gets shorter, and the short 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 that does the arithmetic and then the attribution.

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

3

It’s exactly the same, you can use either, are only preferences! Some say it also touches a bit on code understanding (a = a+2 be easier to understand than a+=2) but don’t worry too much about it.

2

There is no conceptual difference between the two. In both cases, you are defining that a becomes worth the current value of a plus 2, which you correctly claimed to be 12.

The term "receive" is common in programming and does not necessarily have to do with this compound operator +=, but yes with any operation in which you assign a value to a variable. When you say "a receives a + 2", is just a way of saying that this value is being "assigned", "defined", "allocated" in the variable a.

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

Browser other questions tagged

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