Search code examples
javanotation

Understanding the ? notation for Java


I came across a code snippet online that used a notation that from what I gather seems to do a comparison and then returns back possible multiple outputs. I am still confused about it, even after research. Can someone re-write the code snippet to an equivalent, more basic version so that I can make sure I am understanding what I am seeing?

int mPart = i < mParts.length ? Integer.parseInt(mParts[i]) : 0;

Thanks in advance!


Solution

  • This is ternary IF operator. This line is equal to

    int mPart;
    if(i < mParts.length) {
       mPart = Integer.parseInt(mParts[i]);
    } else {
       mPart = 0;
    }