SOS
SOS

Reputation: 131

Missing value in Java

What is the statement that can be used in Java to represent a missing value of a variable. for instance I want to write a code:

if (a>=23)
 income = pay_rate;
else 
 income is missing;

Upvotes: 11

Views: 6006

Answers (11)

Eldar
Eldar

Reputation: 5237

You can do it following way:

public abstract class Nullable extends Number {

    protected final boolean isNA;


    protected Nullable(boolean isNA) {
        this.isNA = isNA;
    }

    public boolean isNA() {
        return isNA;
    }
}


public final class NullableInt extends Nullable {

    public static final NullableInt NA = new NullableInt(0, true);

    private final int value;

    public NullableInt(int value, boolean isNA) {
        super(isNA);
        this.value = value;
    }

    public static NullableInt of(int value) {
        return new NullableInt(value, false);
    }

    public int getValue() {
        if (!isNA) {
            return value;
        }
        throw new RuntimeException("Value is NA");
    }

    @Override
    public int intValue() {
        return getValue();
    }

    @Override
    public long longValue() {
        return getValue();
    }

    @Override
    public float floatValue() {
        return getValue();
    }

    @Override
    public double doubleValue() {
        return getValue();
    }
}

public class Test {

    public static void main(String[] args) {
        NullableInt[] nullableInts = new NullableInt[3];
        nullableInts[0] = NullableInt.of(1);
        nullableInts[1] = NullableInt.of(2);
        nullableInts[2] = NullableInt.NA;

        System.out.println(Arrays.toString(nullableInts));
    }
}

Upvotes: 0

missingfaktor
missingfaktor

Reputation: 92066

There is a lot of bad advice in this thread. First let me address why you should not be going for some of the suggested approaches.

1. Using wrapper types and null

Integer income = null;
if (a >= 23) {
  income = payRate; 
}

Java has auto-unboxing. What if you somewhere accidentally use income in a place where Java had to auto-unbox it? The compiler cannot catch this error and your code will blow up at runtime.

Secondly, the "nullability" of income is not part of income's type. As a result, it's up to the programmer to be careful about checking it for null every time it is used. If he forgets to perform this check, compiler will not complain, but you will get NullPointerExceptions at runtime.

2. Using sentinel values to denote exceptional conditions

int income = Integer.MAX_VALUE;
if (a >= 23) {
  income = payRate;
}

This approach shares the second drawback of null approach. Even worse, in this case, instead of throwing an exception, computation will continue even in erroneous conditions, since Integer.MAX_VALUE is a valid int, leading to possible disastrous outcomes.


So how should you deal with this?

Use a data type that:

  • has "nullability" expressed in its type.
  • does not involve conditional checks such as in above two approaches.
  • ensures at compile time that the data is not being accessed in illegal state.

Maybe (also known as Option) fits the bill. (@Bahribayli already suggested this. I am expanding on it.) See the data definition here.

And this how you would use it in your case:

Maybe<Integer> income = Nothing.value();
if (a >= 23) {
  income = Just.of(payRate);
}

There is a library named Functional Java that provides this abstraction. If you have any further questions regarding the use of this data type, I will be happy to answer them.

Upvotes: 14

John Woo
John Woo

Reputation: 263803

You need to Initialize varible with null

Double income = null;
if (a>=23) 
{
   income = pay_rate; 
}

Upvotes: 0

Bahribayli
Bahribayli

Reputation: 346

Option type in type theory is a way to specify type of a variable that may or may not have a meaningful value. Some languages support Option type like Scala's scala.Option type. For languages that don't support the type you can use wrapper classes or boxed counterparts for primitives.

public class Income {
  private int value;
  public Income(int value) {
    this.value = value;
  }
  public int getValue() {
     return value;
  }
  public void setValue(int value) {
    this.value = value;
  }
}

...
Income income = null;
if( a >= 23)
  income = new Income(pay_rate);

or simply

Integer income = null;
if( a >= 23)
  income = pay_rate;

Upvotes: 3

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726809

You are looking for a value, not for a statement, and that value is null. You cannot assign null to variables of primitive data types, such as int and double, but you can use it with their boxed counterparts -- Integer, Double, et cetera.

When you do that, you should be careful to check your values for null before accessing them. Otherwise, an innocent-looking if (pay_rate > 100) may throw a null reference exception.

Upvotes: 6

Crazenezz
Crazenezz

Reputation: 3456

Use throws:

if (a>=23)
    income = pay_rate;
else 
    throw new IllegalArgumentException("income is missing");

Upvotes: 2

havexz
havexz

Reputation: 9590

As mentioned in other ans here, you can use a boxed type. But I ll recommend look for the valid values for your data and choose any invalid value for missing. In your specific case of income, which cannot be negative, so you can choose -1.

The reason to avoid boxed type is performance. I am not against using them but if you can work without them then there is no need to use them.

For the cases where most of +ve and -ve values are valid values, use eitherInteger.MAX_VALUE or Integer.MIN_VALUE as your missing value. You ll just loose one value of all the available value range.

NOTE: Auto-boxing (converting implicitly from primitive to boxed type and vice-versa) is a good feature but can lead to some performance issues which are difficult to debug and find.

Upvotes: 2

Epicblood
Epicblood

Reputation: 1177

you could try this as well, maybe just the way I like to do it, but up to you :p

if(a >= 23){
    income = payRate;
} else{
    income = null;
}

rather than setting it to null by default, you set it to null after it checks what a is. also, make sure income is either an Integer or Double

Upvotes: 0

Jigar Joshi
Jigar Joshi

Reputation: 240928

Double income = null;
if (a>=23) {income = pay_rate; }

income would be null by default so if uninitialized

Upvotes: 2

Kshitij
Kshitij

Reputation: 8624

you can set income to null. Later, when you check value of income, if it is null, than treat it as missing

Integer income = a>=53?pay_rate:null;

Upvotes: 2

Jonathan W
Jonathan W

Reputation: 3799

Integer income = null;
if (a >= 23) {
  income = payRate; // underscores are generally considered bad convention in Java
}

Upvotes: 2

Related Questions