Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Evaluating multiple variable together in if condition

I was wondering whether its possible in java to evaluate multiple variables together in if-else condition like in python.

actual code

if(abc!=null && xyz!=null)
{//...}

dummy code

if(abc && xyz !=null)
{// will it be possible}
like image 643
A Gupta Avatar asked Jun 04 '13 06:06

A Gupta


People also ask

How do you check multiple items in an if statement?

Here we'll study how can we check multiple conditions in a single if statement. This can be done by using 'and' or 'or' or BOTH in a single statement. and comparison = for this to work normally both conditions provided with should be true. If the first condition falls false, the compiler doesn't check the second one.

Can we use two conditions in if statement?

Use two if statements if both if statement conditions could be true at the same time. In this example, both conditions can be true. You can pass and do great at the same time. Use an if/else statement if the two conditions are mutually exclusive meaning if one condition is true the other condition must be false.

How do you test multiple variables for equality against a single value?

If you have the opposite case and you have multiple variables you need to check against one value, you can swap the left and right sides of the in operator. So instead of using or operators like this: >>> a, b, c = 3.1415, 'hello', 42 >>> if a == 'hello' or b == 'hello' or c == 'hello': ...

Can an if statement have multiple conditions Python?

In Python, we can use logical operators (i.e., and, or) to use multiple conditions in the same if statement. Look at the code below.


3 Answers

FIRST DRAFT

You can write smth like this:

boolean notNull(Object item) { 
    return item != null;
}

then you could use it like:

if (notNull(abc) && notNull(xyz)) {
    //...
}

UPDATE 1:

I came up with a new idea, write function using varargs like:

boolean notNull(Object... args) {
    for (Object arg : args) {
        if (arg == null) {
            return false;
        }
    }
    return true;
}

usage: (you can pass to function multiple arguments)

if (notNull(abc, xyz)) {
    //...
}

UPDATE 2:

The best approach is to use library apache commons ObjectUtils, it contains several ready to use methods like:

  • allNotNull(Object... values),
  • anyNotNull(Object... values)
  • or firstNonNull(T... values)
like image 114
jtomaszk Avatar answered Nov 27 '22 01:11

jtomaszk


the only way this would work is if abc was a boolean (and it wouldn't do what you're hoping it would do, it would simply test if abc == true). There is no way to compare one thing to multiple things in Java.

like image 38
Yevgeny Simkin Avatar answered Nov 27 '22 03:11

Yevgeny Simkin


It's Impossible in java, you can use Varargs:

public boolean  checkAnything(Object args...){
  for(Object obj args){
    if(...)
  }
  return ....;
}

See also:

  • Varargs

  • String… parameter in Java

like image 27
Rong Nguyen Avatar answered Nov 27 '22 01:11

Rong Nguyen