-2

I have no idea what does this mean:

this.x = x < 0 ? 0 : x;
this.y = y < 0 ? 0 : y;

I could not find the meaning of these operators, any help will be greatly appreciated!

Vivi Xu
  • 131
  • 2
  • 5
  • 12

9 Answers9

7

Yes. That Terinary (or Conditional) Operator in java. A short hand for if and else condition.

The code this.x = x < 0? 0 : x; equivalent to

if (x<0) {
    this.x = 0
} else{
    this.x =x
}
Olimpiu POP
  • 5,001
  • 4
  • 34
  • 49
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • 2
    +1.. Although *Jon Skeet* prefers *Conditional operator* instead of *ternery*. :P – TheLostMind Sep 12 '14 at 10:23
  • @TheLostMind Even me prefer after your comment ;) – Suresh Atta Sep 12 '14 at 10:24
  • 2
    @SlodgeMonster The [JLS](http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.25), however, calls it the *conditional operator*, so I'd say it's the official name for it in Java. – kiheru Sep 12 '14 at 10:29
  • @SlodgeMonster That is very bad if you referring to Jon Skeet. I don't bother if you are referring to me :) – Suresh Atta Sep 12 '14 at 10:34
2

Your class has a field named x. this.x is used to refer to that field unambiguously: you need to do this if there is a local x in your scope.

x < 0 ? 0 : x; is an idiom that exploits the ternary operator. It evaluates to no less than zero.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
2

In this case:

this.x = x < 0? 0 : x;

This means that the value of x is dependent on the condition (the one before the question mark x < 0), the value of x is the first (the value before the : which is 0) if the condition evaluates to true, else the second value (the value after the : which is x itself).

Also, The value of x is equal to 0 if it is a negative number, other than this case the value is x itself.

1

It means

if (x < 0) {
   this.x = 0;
} else {
   this.x = x;
}

Which basically means this.x = Math.max(0,x).

Eran
  • 387,369
  • 54
  • 702
  • 768
1

This is the ternary if operator and basically is equal to:

if(x < 0){
    this.x = 0;
else
    this.x = x;

same thing for the y

and its syntax is condition ? if_part : else_part;

gkrls
  • 2,618
  • 2
  • 15
  • 29
1

it means that:

if (x < 0) {
        this.x = 0;
    } else {
        this.x = x;
    }

the same for y;

Rafik BELDI
  • 4,140
  • 4
  • 24
  • 38
1

The conditional operator (?:) returns one of two values depending on the value of a Boolean expression.

Syntax: condition ? first_expression : second_expression;

this.x = x < 0? 0 : x;
this.y = y < 0? 0 : y;

Means if x<0 if true then it will return 0 else return x So the value of x is depend on the conditions.

Refere ?: Operator .

Hope this may help you!

Krupa Patel
  • 3,309
  • 3
  • 23
  • 28
1

It's called Ternary Operator.

(condition) ? [if true]
            : [if false]

In your case:

this.x = x < 0? 0 : x;
this.y = y < 0? 0 : y;
  • If x is less than 0, assign 0, else assign its value
  • If y is less than 0, assign 0, else assign its value
lxcky
  • 1,668
  • 2
  • 13
  • 26
1

It's the shorthand conditional operator.

The statement int n = x > 0 ? x : 0 will set n to x if x > 0 returns true. Otherwise, n is set to 0.

astronomotrous
  • 431
  • 1
  • 3
  • 12