dimanche 21 juin 2015

how does Java handle && inside if statement

I don't understand the reason why the follow java code throws a ArrayIndexOutofBound exception

    while (intArray[j] < intArray[j - 1] && j > 0) {
        int temp = intArray[j - 1];
        intArray[j - 1] = intArray[j];
        intArray[j] = temp; 
        j--;
    }

It appears that even if I AND(&&) both conditions, it will execute one of the conditions first to check if it qualifies the condition, in this case, it will be intArray[0] < intArray[-1] and cause out of bound exception. Which confuses me at first, now I just wandering, why does the program behave that way?

Aucun commentaire:

Enregistrer un commentaire