0

I have below code, which i am checking not null check for two variables.

 if (userParameter != null || configurationParameter != null){
   add.userParameter();
   add.configurationParameter();
 }

Is there any way I can check for not null in one ifcondition like if((userParameter or configurationParameter) != null) in java.

Amit Bhati
  • 5,569
  • 1
  • 24
  • 45
user3297076
  • 85
  • 1
  • 8
  • 1
    What are you trying to archieve with that? – Albjenow Jan 16 '17 at 08:45
  • yeah question is unclear – Faraz Jan 16 '17 at 08:45
  • 3
    Not strictly related but I think that in your code there should be && operator. If both are not null then operate, am I right? – zubergu Jan 16 '17 at 08:46
  • I would suggest to replace the ``||`` with ``&&``, but i honestly don't understand your question. – f1sh Jan 16 '17 at 08:46
  • do these two variables belong to any class/object? if yes then you only check if that object is null. Like lets say userParameter and configurationParameter belong to Test class, then you only test if test is null like this: if (Test != null) ... – Faraz Jan 16 '17 at 08:46
  • @Faraz Durrani The check on the both variables should be performed at a time or at another time. – davidxxx Jan 16 '17 at 08:47
  • with something like this `public static boolean isNull(Object... args)` – AxelH Jan 16 '17 at 08:48
  • 1
    Possible duplicate of [How to check multiple objects for nullity?](http://stackoverflow.com/questions/31582524/how-to-check-multiple-objects-for-nullity) – Magnilex Jan 16 '17 at 08:52

5 Answers5

7

There is no way to compare null(single) thing with multiple things(i.e. userParameter & configurationParameter) in java.

But you can write a method like below, use it when you are checking multiple values for null with || :-

public boolean isNull(Object... args){
for(Object arg: args){
  if(arg==null) return true;
}
return false;
}
Amit Bhati
  • 5,569
  • 1
  • 24
  • 45
3

As you have two parameters, the most you can do is to create a method that return true or false depending on the check:

 if (checkForNull(userParameter, configurationParameter)){
   add.userParameter();
   add.configurationParameter();
 }

 private boolean checkForNull(Object... objects){
    for (Object obj : objects){
        if (obj == null){
            return true;
        }
    }
    return false;
 }

EDITED

As @shmosel and @AxelH proposed, The method would be more effiecient using varargs. Thanks for the improvement

Rafa Romero
  • 2,667
  • 5
  • 25
  • 51
  • 1
    If you're going with helper methods, might as well go all out: `boolean checkForNull(Object... objects) { for (Object obj : objects) if (obj == null) return false; return true; }` – shmosel Jan 16 '17 at 08:48
  • 1
    Same comment, using varargs made this way more usefull. ( I have just wrote this in comment section) – AxelH Jan 16 '17 at 08:49
  • 2
    You will only check the first argument with this. The `return true` should be after the loop, when you are sure none are `null` ;) – AxelH Jan 16 '17 at 08:57
  • I wasn't suggesting varargs would be more efficient, just more useful. – shmosel Jan 16 '17 at 08:58
  • 1
    `checkForNull` can be a confusing name for a method that checks for no nulls. Also, you're missing a closing parenthesis on the first line. – shmosel Jan 16 '17 at 09:00
3

In some cases, your current code already will only be checking one condition, because the || operator short-circuits. It is the case that if the condition userParameter != null be true, then the JVM won't even check the other condition.

Besides this, there is no way as far as I know to check for null in two separate varibles in a single comparison.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
2

Yes, there is the ternary operator ? (the conditional operator):

 if (userParameter != null ? configurationParameter != null : false) {

(Note: The logic is different from your code, you probably want to check if both are not null)
However, using the features everybody else is using too is probably a good idea.

alain
  • 11,939
  • 2
  • 31
  • 51
  • 1
    This is identical to `if (userParameter != null && configurationParameter != null) {`. As a rule of thumb, you should never need a boolean literal in a ternary operation. – shmosel Jan 16 '17 at 08:51
  • Nit: its correct name is the [*conditional operator*](https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.25), which is *a* ternary operator (and currently the only ternary operator). – Andy Turner Jan 16 '17 at 08:53
  • @shmosel Yes, I know the logic is different (see the note I added) because I think this is what the OP should use. And I completely agree this should not be used in real code (see my last line). – alain Jan 16 '17 at 08:55
  • I didn't complain about the logic. My problem is with the unnecessary use of a ternary operator. – shmosel Jan 16 '17 at 08:57
0

You may Use Binary Operator Check Here

or you can use the ternary Operator.

Zia
  • 1,001
  • 1
  • 13
  • 25