'explanation of variable setting formula in Java required

i have this java code:

weight = weight == 0 ? 1 : weight;

Can anyone please explain what will happen to variable weight in all cases?



Solution 1:[1]

If weight equals 0, then weight will be set to 1, else it remains unchanged.

Many people don't like the ternary ? operator, because it is harder to read. Those people would write

if (weight == 0) {
    weight = 1;
}

And they have a point there, I'd say.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ronald