Can you store a variable inside a if-clause?

后端 未结 6 1583
不思量自难忘°
不思量自难忘° 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:42

    Actually I think that is a way to do this using Java Optional, here's an example:

    Optional.ofNullable("text").ifPresent( $0 -> {
      System.out.println($0.toUpperCase());
    });
    

    Or in your case:

      if(foo != null){
          Optional.ofNullable(foo.getBar()).ifPresent(bar -> {
              System.out.println("Success: " + bar);
          });
      }
    

提交回复
热议问题