Any difference between the two expressions?

Asked

Viewed 133 times

2

int p=4,u=12;

System.out.println(p=u);
System.out.println(p=+u);

I don’t understand the difference between the two expressions?

  • 2

    They will print the same result the first will assign 12 to p, and in the second case it gives anyway because the signal in this case does not influence anything ( if it was the negative signal ai influenced and the value of p would be -12 in the next line)!

  • Having this or the variable multiplied by -1 (p=u*-1) in the case (p=-u) means exactly the same

  • Operator overload and signal multiplication ,for the machine is different but the result will always be the same. But computationally which takes longer to run?

2 answers

4

In the first impression there is only one assignment the p receives the value of u

int p=4,u=12;
System.out.println(p=u);
Resultado: 12

In Second Impression there is an attribute but you make use of the unary operator + on the operand u:

int p=4,u=12;
System.out.println(p=+u); //é o mesmo que p = +(u) OU p = 1(u)
Resultado: 12

With the above values it is difficult to notice the difference, but in the example below note the change:

int p=4,u=-12;
System.out.println(p=-u); //é o mesmo que p = -(u) OU p = -1(u)
Resultado: 12

Here is a short explanation of the unary operator:

a = -b;

In the above example b is the operand over which the operator - acts.

  • 2

    I would not say "indicating the sign". For if u=-15, p will be worth -15 after the assignment. The most correct would be to state that it is the use of the unary operator +

  • 2

    @Jeffersonquesado took the words from my keyboard. Let me know when you’re done editing.

1


In theory it is quite different. One makes an assignment of a value contained in a variable to another variable. The other operates the value of a variable stating that it wants to apply a positive to it.

In practice it is the same thing. When one uses the operator of "more" unary (which is different from the binary where one makes a sum), it is innocuous.

Do you remember math class? Positive with positive gives positive, positive with negative gives negative, that is, in this case it will always give the very value that it is being applied.

It is certain (I doubt that any compiler works differently) that the language will make the + a null operation and in execution there is no instruction that does anything because of the unary operator, then the performance will be the same.

Some say that languages should not even have this operator since it does not do something useful. Some say that it can give a better semantics to the intended, even if it does not change the result.

The unary operator of negative already influences, since if the value is negative it becomes positive, according to the same math class.

It is not recommended to use something that has side effect like expression. And this code does this. It calculates and assigns a value and already uses it as an argument of the method that will make the impression. This code would be better written as:

int p = 4, u = 12;
p = u;
System.out.println(p);
p = +u;
System.out.println(p);

I put in the Github for future reference.

But it depends on the context. In this example I wouldn’t even need it. In some I see no problem using attribution as an expression.

Nor will I enter into the question having a syntax error and the intention was to do something else.

  • But in the case of attribution by means of the negative sign (p=-u) what the compiler does is multiply by -1 the value of "u" and write p the result. Or the assignment is done based on the operator (-). What I mean is whether the machine interprets the expression (p=-u) and (p=u*-1) in the same way

  • That’s not in your question. The compiler does not multiply anything by -1 in any case unless your code sends (even this may not be done by the compiler). I don’t know what the last sentence means.

  • (p=-u) and (p=u*-1) computationally which the most efficient?

  • It depends on the implementation. If there is no optimization, it is the first.

  • Then in the first expression the machine inverts the bits of the variable and writes the result in "p" and the second expression and something more computationally complex?

  • No, depending on the implementation, you can do it in many different ways. Even the processor itself can optimize something.

Show 1 more comment

Browser other questions tagged

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