0

I have the primitive variable notional of double type:

private double notional;

Now I have to do a null check and throw an Exception if it is null:

if (item.getNotional() == null) {
    throw new Exception("Rfgygf");
}

But this is cannot be the correct approach since primitive types cannot be null. Please explain how to perform a proper null check on notional in Java.

Is there a way to do this with a with double or do I need to convert it into Double?

4
  • 2
    Primitive type cannot be null. Why? Nov 17, 2015 at 4:24
  • @hfkjflhfjgdjdhkd You can set a "per say" null value so you know its null when its a specific value.... like most comonly used -1 but this in no way means it is null just saying
    – 3kings
    Nov 17, 2015 at 4:25
  • @YeldarKurmangaliyev yeah but is there any way to overcome from this now Nov 17, 2015 at 4:26
  • So, you have a field of type double. Now, you want to check if it is null, but you know that it cannot be null. It simply doesn't make sense. Why should you do this? What do you mean by "if it is coming as null"? Nov 17, 2015 at 4:28

4 Answers 4

7

Primitive data types such as double and int cannot be null. It's like an int cannot be 1.1.

Which means, you don't need to worry about nullity in primitive data types! They are never null and thus will not throw NullPointerExceptions. I guess the reason why you have this confusion is that you think a variable without a value must be null. But that is wrong. Consider this class:

public class MyClass { int i; }

And now you create an object of the class:

MyClass obj = new MyClass();

Now you print obj.i. What will it be? It's not null, it's 0!

All primitive data types have a default value. For instance, The default value of int is 0, the default value of double is 0.0, which explains why the above obj.i is 0. And what is the default for char? It's '\0'!

Just remember that primitive data types cannot be null.

Never

Never

Never

1

A primitive type cannot have a null value. Only reference types can have a null value. When you first declare a double, the value stored inside is a 0. Basically, objects of type double will always store a value. If you don't specify what that value is, it will be a 0.

0

When you say

private double notional;

It is not null, it is 0.0d. Primitive values cannot be null.

JLS-4.12.1. Variables of Primitive Type says (in part)

A variable of a primitive type always holds a primitive value of that exact primitive type.

And null is not a primitive value. The explanation can be found at JLS-4.12.5. Initial Values of Variables which says (in part)

For type double, the default value is positive zero, that is, 0.0d.

0

As the other answers have indicated, primitive data types cannot be null.

I suggest you look at the Optional class as an alternative to using nulls to represent no value. Java 8 includes Optional and there are similar versions in the guava library.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.