Two conditions in one if statement does the second matter if the first is false?

前端 未结 6 1191
日久生厌
日久生厌 2020-12-04 23:52

Okay, so I have this piece of code tested and I found there isn\'t any exception thrown out.

public static void main(String[] args) {
    int[] list = {1,2}         


        
6条回答
  •  难免孤独
    2020-12-05 00:22

    Yes, both usages are semantically equivalent... That is, unless the length is > 2, then is the code allowed to access more than the 2nd element.

    In the first case, what matters is the && operator, think of it as: If first condition is true then apply second condition.

    The subsequent condition also has an implicit "then" clause

    if length greater than two
     then access third element
    

提交回复
热议问题