Are the method calls inside if-checks “tested” and then “reverted”?

后端 未结 2 796
遇见更好的自我
遇见更好的自我 2021-01-29 10:15

I have suddenly forgotten how method calls inside if-checks works.

Example:

if (list.next() instanceof AClass) {
    AClass thing = list.next();
}
         


        
2条回答
  •  没有蜡笔的小新
    2021-01-29 10:46

    If list is an Iterator, you may need something like:

    AClass thing = null;
    Object n = list.next();
    if (n instanceof AClass) {
         thing = (AClass) n;
    }
    

    in order to call .next() just once. Two subsequent calls will return two objects, because internal position is moved one step forward on each .next() invocation. There no any kind of "reversion" occurs.

提交回复
热议问题