Can you store a variable inside a if-clause?

后端 未结 6 1581
不思量自难忘°
不思量自难忘° 2021-01-01 16:21

I\'m kinda waiting for a \'no\' answer on this question.

I was interested if you can save a variable at the same time when you checking it in an if-clause.

6条回答
  •  时光说笑
    2021-01-01 16:35

    From the department "My Programming Language is Better Than Your Programming Language": In Groovy, you can use the "?." operator:

    Bar bar = foo?.bar
    if (bar != null) {
    }
    

    In Java, this is good pattern(*):

    Bar bar = foo == null ? null : foo.getBar();
    if (bar != null) {
    }
    

    *: Something you can save in your fingertips.

提交回复
热议问题