Can you store a variable inside a if-clause?
Asked Answered
F

8

24

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.

Let's say I have this code.

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

I handling now to "failure" -states independently, even if the outcome is the same. I could get them together like this:

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

Much neater code already. if foo is null it will stop there and won't try foo.getBar (in the if) so I won't get a NPE. The last thing i would like to enhance, and the main question: Do I really gave to call on foo.getBar() twice? It would be nice to get away from the second identical call if getBar() would be a very heavy operation. So I am wondering if there is somehow possible to do something similiar to this:

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

I would have to break it up to two different if's again if I would like to do

Bar bar = foo.getBar();
if (bar!=null) ...
Futurism answered 19/2, 2009 at 7:47 Comment(0)
U
39

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.");
}
Unpen answered 19/2, 2009 at 7:53 Comment(2)
Yeah, this actually worked. I tried to have (Bar bar = foo.getBar()) !=null inside the if, but java doesn't seem to like to have the Object's initialization inside the if-clause. Thanks!Futurism
@zafar142003 it works exactly the same with primitive variables.Unpen
S
14

I have used that technique when iterating over lines from a BufferedReader:

BufferedReader br = // create reader
String line
while ((line = br.readLine()) != null) {
    // process the line
}

So yes, you can do an assignment, and the result off that will be the left hand side variable, which you can then check. However, it's not legal to declare variables inside a test, as they would then only be scoped to that expression.

Schmid answered 19/2, 2009 at 7:52 Comment(0)
C
7

if you want to limit the scope of Bar bar I'd add { and } around the code that Michael posted.

void foo()
{
    // some code ...
    
    // this block limits the scope of "Bar bar" so that the rest of the method cannot see 
    // it.
    {
        Bar bar;
        if(foo!=null && (bar = foo.getBar())!=null){
            System.out.println("Success: " + bar);
        } else {
            System.out.println("Failiure.");
        }
    }
}

You might also want to check into the null object pattern if it makes sense. I personally try to avoid things being null if I can... really think about if you want null to be allowed or not.

Centreboard answered 19/2, 2009 at 8:7 Comment(0)
D
2

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.

Diminish answered 19/2, 2009 at 10:34 Comment(0)
N
2

Three points that completely fail to answer the question:

null is evil. Don't write methods that return it. Your example problem would then disappear.

I think you might be missing out on encapsulation. Instead of foo.getBar() could the interface of foo be made such that you perform a "tell don't ask" operation?

Side-effects in expression tends to cause bad code. Prefer more, simpler lines to fewer, buggy lines. The usual exception if using ++ to increment an index when accessing a buffer, or similar iterator style algorithms.

Nonmetallic answered 19/2, 2009 at 13:6 Comment(0)
E
0

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);
      });
  }
Earthaearthborn answered 22/3, 2018 at 14:2 Comment(0)
M
0

You cannot declare a variable in a Java conditional, but you can in a for loop:

for (Bar bar = (null != foo) ? foo.getBar() : null; bar != null;) {
    System.out.println("Success: " + bar);
    break;
}

But it is not pretty. I completely agree with Tom Hawtin's comments.

Mathi answered 21/7, 2021 at 22:59 Comment(0)
C
0

With pattern matching in Java 14, you can get even closer:

if(foo != null && foo.getBar() instanceof Bar bar) {
    // instanceof itself does null-check
    System.out.println("Success: " + bar);
} else {
    System.out.println("Failure.");
}
Cogwheel answered 2/6 at 17:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.