3

Can someone please explain, in simple English, the logic behind this statement?

return mContainsLoadingRow ? (getContentDataSize() + 1) : getContentDataSize();

Assuming mContainsLoadingRow is a boolean, if mContainsLoadingRow is true,

then return getContentDataSize() + 1.

If not, return getContentDataSize().

Is that the correct way to look at this?

Vikrant Kashyap
  • 6,398
  • 3
  • 32
  • 52
Martin Erlic
  • 5,467
  • 22
  • 81
  • 153
  • 1
    Yes ist is. You can also write it as `if(mContainsLoadingRow ) return getContentDataSize() + 1 else return getContentDataSize()` – Jens Jul 06 '16 at 11:27

2 Answers2

3

this complete expression is know as Ternary Operator in Java.

Code Statement

mContainsLoadingRow ? (getContentDataSize() + 1) : getContentDataSize();
        ||                       ||                         ||
 //boolean expression      //return if true          //return if false

here in this code

mContainsLoadingRow is a Boolean variable which contains either true or false. you can also change mContainsLoadingRow with any Boolean expression like (a>b or b==a or b <= a etc.)

? (question mark) :- enables us to fine whether it is true or false.

if true the expression (getContentDataSize() + 1) will be return.

if false then expressin getContentDataSize() value will be return.

Vikrant Kashyap
  • 6,398
  • 3
  • 32
  • 52
  • Also a different way of writing this would be "return getContentDataSize() + mContainsLoadingRow ? 1 : 0; " – Trap Oct 16 '20 at 07:35
0
int x = 0;
if (0 < 1){
  x = 2;
}else{
  x = 42;
}
// in short:
x = (0<1) ? 2 : 42;

So yes, you are right

rmbl
  • 50
  • 8