Question

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?

Was it helpful?

Solution 2

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

i *= -1;
i = -i;

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top