How to check if a double is null? - java

I'm querying a database and some of the results I'm getting are null. I'm setting these values to a variable with a datatype of double. Let's call the variable "results". So I tried setting up an if statement to see it equals Null which of course didn't work. Here is the code I have for that if statement:
if (results == null)
{
results = 0;
}
The error I get with this code is:
The operator == is undefined for the argument type(s) double, null
Is there a better way to determine if it's null?

Firstly, a Java double cannot be a Java null, and cannot be compared with null. (The double type is a primitive (non-reference) type and primitive types cannot be null.) I'm therefore assuming the following:
The "null" you are trying to detect is a NULL stored in the database that you are querying.
You are using JDBC to query the database.
If you call ResultSet.getDouble(...), that returns a double not a Double, the documented behavior is that a NULL (from the database) will be returned as zero. (See javadoc linked above.) That is no help if zero is a legitimate value for that column.
Changing the declared type of result to Double instead of double as suggested by Toby's answer will not help. When the database contains NULL, the value assigned to result will be a Double(0.0), not a null.
Here are two options that will work:
Use ResultSet.wasNull() to test for a (database) NULL ... immediately after the getDouble(...) call.
Use ResultSet.getObject(...), and test for null using ==.
The getObject method will deliver a (non-NULL) value as a Double (assuming that the column type is double). It is documented to return null for a NULL. (For more information, this page documents the default mappings of SQL types to Java types, and therefore what actual type you should expect getObject to deliver.)

I would recommend using a Double not a double as your type then you check against null.

A double primitive in Java can never be null. It will be initialized to 0.0 if no value has been given for it (except when declaring a local double variable and not assigning a value, but this will produce a compile-time error).
More info on default primitive values here.

To say that something "is null" means that it is a reference to the null value. Primitives (int, double, float, etc) are by definition not reference types, so they cannot have null values. You will need to find out what your database wrapper will do in this case.

How are you getting the value of "results"? Are you getting it via ResultSet.getDouble()? In that case, you can check ResultSet.wasNull().

I believe Double.NaN might be able to cover this. That is the only 'null' value double contains.

Related

How can I modify the default IntegerTypeHandler in Mybatis

I try to use <select> to query int value in mysql with mybatis, it return null when nothing matches.
Howerver the corresponding method in XXXMapper.java return the primitive type int, which case exception: attempted to return null from a method with a primitive return type (int).
So i want to modify the default IntegerTypeHandler in mybatis, i want MyConfigIntegerTypeHandler returns -1 when nothing found.
I know the best way to solve this problem is to modify the method defination, to make the query method return Integer rather than int. I just want to know how to fix it without corrent the origin code.

Using Integer value in MDC object

I was using MDC for logging in my spring boot application. Along with that I was also maintaining a graph. I was fetching the values from MDC to draw the graph. It requires Integer values not the string values. I don't want to make use of String values and then type caste it explicitly after fetching the values for the graph reason being the code may break during type casting as it may contain null values (even though there would be no such scenario as I have kept the default value as "0" but still I want to avoid this).
If I try
MDC.put("Speed", "0");
It works fine.
But it shows error for
MDC.put("Speed", 0);
Please tell how can I put an integer value in the MDC object.
MDC only supports string values as keys and values. What you can do instead, to avoid type casting errors for null values, is to use Integer class instead of the primitive int.

How to set empty value?

My problem is that I don't know how to set empty value. Because first 3 values must be empty.
I tried to set NaN or just didn't print value then it does not pass the test.
So, how I set empty value? It is possible or I just don't understand my assignment?
It's an assignment for Weighted Moving Average.
It's must look like this but as I said I don't know how to print empty value.
Thanks for the help.
What you call empty depends on your problem. In Java, empty values are usually reflected as null. The problem is that you cannot use nulls with primitive types such as double. You probably want to use a wrapper type, java.lang.Double in your case.
For example :
Double emptyValue = null;

long Value assigned with Default Constructor in Java

I need to put a check for object elements to see if they are null or blank or are having their default values. I printed default value of a long element and it turned to be 0. In a review i have been asked to put a check for the long element to be greater than 0 too.
Will default object constructor construct a object with negative value for any of the member element.
Will default object constructor construct a object with negative value
for any of the member element.
No.
The Java class-members have default values as follows:
int, long, short, char, byte default to 0.
boolean defaults to false.
Non-primitive members default to null.
Have in mind that the Wrapper implementations will default also with null, because their instances are objects, not primitives.
In Java all values are initialized with null or 0 or false (whatever is applicable), so a check for this is as simple as value != null or value != 0.
The more important question is: why do you actually need to check this during construction? Because at this time, the value is equal to what you set it in your code, and no external function could have modified it at this time.
No there is no variable initialize with garbage value in JAVA, you will definately get the default value if you have not initialize the variables in object
like for boolean there is default value "false"
for int default value is 0
and Note all the instance (objects like non-primitive data types) are intialized with default value of null
Garbage collector in JAVA done that work for you of default initialization
In Java long is one of the primitive types.
When used as a class field without explicit initial value, it'll be assigned 0.

How to Pass NULL Data Type?

What is the datatype for NULL when passing that value for no data into a database?
There is NO data type of NULL. NULL itself means ABSENCE of data. When there is no data, how can it have type?
Null does not have a specific data type in SQL. Any nullable column or variable can contain null. Null is never equal or unequal to anything. You can cast a variable holding null to another variable and get null, for example:
declare #a integer
set #a = null
select convert (float, #a)
----------------------
NULL
(1 row(s) affected)
Usually NULL is its own datatype - the type of 1 is "INTEGER", the type of the type of NULL is "NULL"
Datatype for NULL is as meaningless as datatype for 0: it can be INTEGER, FLOAT or a VARCHAR. You cannot tell it just from the value.
NULL is legitimate value in almost every datatype domain, which means the absence of actual value.
It's also meaningless to discuss datatypes out of context of certain RDBMS.
In SQLite, for instance, datatypes are value-bound, not column-bound, and NULL is a first-class datatype per se.
In Oracle, the datatypes are more strictly defined. For instance, this query works:
SELECT COALESCE(dt, i)
FROM (
SELECT CAST(NULL AS DATE) AS dt, CAST(NULL AS DATE) i
FROM dual
) q
and this does not:
SELECT COALESCE(dt, i)
FROM (
SELECT CAST(NULL AS DATE) AS dt, CAST(NULL AS NUMBER) i
FROM dual
) q
, because the latter query returns two columns of different datatypes, both of them having values of NULL, and COALESCE requires both arguments to have same datatype.
It's better to say that a NULL of any datatype can be implicitly converted to a NULL on another datatype.
For instance, a VARCHAR can be implicitly converted to a INTEGER if it has value of 0, but cannot if it has value of 'some_string'.
For NULL's, any datatype can be implicitly converted to any other datatype, if the implicit conversion between them is allowed at all.
In SQL a NULL is a "mark" (something other than a value) that can apply to any SQL type. So it is orthogonal to type.
NULL is the value for 'undefined'. So any type in a database can be 'undefined', as it's a property of the column: a value of a row for the specific column can be 'undefined' which means it's 'NULL', no matter what the type is. As long as the column is nullable.
I think DBNULL or NULL is a special type.
I think the question defeats itself. If NULL had a datatype, wouldn't you be forced to change it with every instantiation outside of its default. For example, when you create it as a character, but then force it into an object's value?
NULL==NULL
That is all.
Actually, in PowerShell comparing $null -eq $null gives False.
Also, -not $null will give you True, so here it seems to be reprezented as False. I know, PowerShell might not be a good example, but still :)
Usually SQL NULL does not have a type associated with it. However there are exceptions. Database engines postgresql and derby (javadb) require that null has a type. In other words they do not support untyped null. So query conditions like NULL IS NULL may fail. Here, NULL must be given a type, the expected type of the target that processes the NULL value. In this case this appears silly because there is no target and this can be counterproductive.
See CAST function: -- you must cast NULL as a data type to use it
See Queries with guarded null Parameter fail and Add support for setObject(arg, null)
Please vote for these issues so that the odd database engines change their ways.
Prabhahar, each type of database driver has its own way of handling NULL. You will have to examine the driver API for the specific database.
For example if you are using the Java Derby database, simply pass in the Java native type, null as shown in Ian Bjorhovde's answer to "Derby's Handling of NULL Values":
insert into T_AUTHOR (
ID, FIRST_NAME, LAST_NAME,
DATE_OF_BIRTH, YEAR_OF_BIRTH, ADDRESS)
VALUES (
1000, 'Lukas', 'Eder',
'1981-07-10', null, null
);
Here is another null example of JDBC:Inserting null to Integer column:
pst.setNull(4, java.sql.Types.INTEGER);
http://en.wikipedia.org/wiki/Null_(SQL)
NULL can be cast (converted) to any data type yet data type comparisons with NULL always return FALSE.

Categories