Pregunta

¿Cuál es la diferencia entre + = y = +? En concreto, en java, pero en general también.

¿Fue útil?

Solución

i += 4;

medios

i = i + 4;  // increase i by 4.

Mientras

i =+ 4;

es equivalente a

i = +4;   // assign 4 to i. the unary plus is effectively no-op.

(Ver http: //docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.15.3 por lo que un unario + hace.)

Otros consejos

+= es un operador que incrementa el lado izquierdo de la asignación por el valor de la parte derecha y la asigna de nuevo a la variable en el lado de la izquierda. =+ no es un operador, pero, de hecho, dos operadores: la = operador de asignación y el operador unario más + (positivo) que denota el valor en el lado de la derecha es positivo. De hecho, es redundante porque los valores son positivos a menos que se niegan a menos unario. Debe evitar el constructo =+ ya que es más probable que cause confusión que hacer ningún bien real.

+= es conseguir y de la subasta:

a += 5; // adds 5 to the value of a

=+ realidad no es un identificador válido por sí mismo, pero puede ser que aparezca cuando se está utilizando el operador unario +:

a =+ 5; // assigns positive five to a

=+ is not an operator. The + is part of the number following the assignment operator.

int a = 4; int b = 4;

a += 1; b =+1;

System.out.println("a=" + a + ", b=" + b);

This shows how important it is to properly format your code to show intent.

+= is a way to increment numbers or String in java. E.g.

int i = 17;
i += 10;  // i becomes 27 now.

There is no =+ operator. But if you do i =+ 10; it means i is equal to +10 which is equal to just 10.

Specifically, in java, but in general also.

In Java x += <expr>; is equivalent to x = x + ( <expr> ); where the + operator may be the arithmetical add operator or the string concatenation operator, depending on the type of x. On the other hand, x =+ <expr>; is really an ugly way of writing x = + <expr>; where the + is unary plus operator ... i.e. a no-op for numeric types and a compilation error otherwise.

The question is not answerable in the general case. Some languages support a "+=" operator, and others don't. Similarly, some languages might support a "=+" operator and others won't. And some languages may allow an application to "overload" one or other of the operators. It simply makes no sense to ask what an operator means "in general".

I don't know what you mean by "in general", but in the early versions of C language (which is where most of Java syntax came from, through C++), =+ was the original syntax for what later became +=, i.e. i =+ 4 was equivalent to i = i + 4.

CRM (C Reference Manual) is the document the describes C language with =+, =-, =>> and so on.

When you have a+=b, that means you're adding b to whatever's already in a. If you're doing a=+b, however, you're assigning +b to a.

int a=2;
int b=5;
a+=b;
System.out.println(a); //Prints 7

a=2;
b=5;
a=+b;
System.out.println(a); //Prints 5
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top