How to check if a double is null?
I would recommend using a Double
not a double as your type then you check against null.
Comparing two nulls, getting NullPointerException
Here's what new Double(null)
does...
The compiler has to find a constructor that matches. There are two constructors for the Double
class (according to the javadoc):
Double(double value)
// Constructs a newly allocated Double object that represents the primitive double argument.
Double(String s)
// Constructs a newly allocated Double object that represents the floating-point value of type double represented by the string.
null
is not a valid value for a primitive type (i.e. small-d double
), so this results in an attempt to call the second constructor, that takes a String
. At runtime, it then tries to parse the string in the format of a double
. And this results in a NullPointerException
because the String
parameter is null. [To be more precise, I think at some point there will be a call to s.trim()
from the class sun.misc.FloatingDecimal
, which throws an exception if s
is null
.]
Inconsistent null equality check scala 2.11.7
Edit: This issue no longer exists in Scala 2.12.6. See pull-request with explanation.
Original answer (for Scala 2.11.7):
null.asInstanceOf[Double] == null
compiles to the:
aconst_null
ifnonnull
The val
-version compiles to the:
aconst_null
invokestatic unboxToDouble
putfield
aload_0
invokevirtual а
invokestatic boxToDouble
ifnonnull
So compiler just forgets to add unbox/box in the first case
Comparison of two null objects from two different types
All null
values are untyped and are equal. You can pass it to different reference types but it makes no difference for comparison purposes.
It is not the null
value which is typed but the reference to the null which can be typed.
A common question is what happens here
class A {
public static void hello() { System.out.println("Hello World"); }
public static void main(String... args) {
A a = null;
a.hello();
System.out.println("a is an A is " + (a instanceof A)); // prints false.
}
}
The compiler sees the type of a
as an A
so the static method is called. But the value referenced is null
and untyped.
The only operations you can perform with null
without causing a NullPointerException is to assign or pass it without examining it or comparing it with another reference.
BTW
In short: The compiler will select a method based on the type of the reference, at runtime the execution is based on the class of the object referenced. At runtime null
is treated as any type or no type or you get a NullPointerException if you try to dereference it.
how to check double value is empty or not
In java double
is a primitive type which can not be null, There is Double
wrapper class in java which can be checked as null.
Declare your coords of type Double
not double
and check if Double
value is null.
Related Topics
Generate Bank Account Number With Random
How to Convert a Kotlin Source File to a Java Source File
How to Check Type of Variable in Java
Is There a Java Setting for Disabling Certificate Validation
Spring Boot - How to Get Running Port and Ip Address
How to Decrypt Whatsapp Database File
Regular Expression (Regex). How to Ignore or Exclude Everything in Between
Jparepository Findall() Returns Empty Result
Dynamically Change Application.Properties Values in Spring Boot
Multipartexception: Current Request Is Not a Multipart Request
Loop Over All Fields in a Java Class
How to View Apache Parquet File in Windows
Jpa: Update Only Specific Fields
Spring Boot: Cannot Access Rest Controller on Localhost (404)
How to Configure Hikaricp in My Spring Boot App in My Application.Properties Files