What does this line means in Java

  • A+
Category:Languages
if (number < 0) number = (number* -1); 

What does this line mean, especially number*, I've never seen before a statement like this in java, I'm new.

 


It looks a bit like a C pointer, but it’s just multiplication.

When properly formatted:

if (number < 0)     number = number * -1; 

This code multiplies number by -1 if number is negative.

The code guarantees number is positive, except if number is Integer.MIN_VALUE, in which case it stays unchanged!

Also, best practice uses braces around the if block:

if (number < 0) {     number = number * -1; } 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: