Can you store a variable inside a if-clause?

后端 未结 6 1627
不思量自难忘°
不思量自难忘° 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条回答
  •  猫巷女王i
    2021-01-01 16:48

    This is the closest you can get:

    Bar bar;
    if(foo!=null && (bar = foo.getBar())!=null){
      System.out.println("Success: " + bar);
    } else {
      System.out.println("Failiure.");
    }
    

提交回复
热议问题