Pregunta

I remember reading somewhere in Java, possibly Oracle documentation that there is a shortcut to assign a flip value of a primitive.

Similar to:

int i = 0;
i += 3;
System.out.println(i);

Output is 3, but what if I wanted -3? Or if given -3, make it positive 3? Rather than doing

i = -i;

Isn't there a shortcut to do it in just the assignment operator?

¿Fue útil?

Solución 2

If you want to reverse the sign of a number you can do one of the following things:

i *= -1;
i = -i;

Otros consejos

You have lots of options:

i = -3;
i += -3;
i -= 3;

Or even

i = 3;
System.out.println("-" + i);

int i = -3; is your easiest solution

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top