Dummy values of serialized variable - java

Why default values as Zero are displayed for transient variables in Java?
How does it know that the value should be 0 for integer and null for String?

It knows that because that's how it's specified. Default value for int is 0, and default value for String is null.

From DOCS
It's not always necessary to assign a value when a field is declared. Fields that are declared but not initialized will be set to a reasonable default by the compiler. Generally speaking, this default will be zero or null, depending on the data type.

Default value of an int is 0 and a String is null

The default values for fields is defined by the JLS. Making it transient gives a hint that it won't be set and thus have the default value.

How does it know that the value should be 0 for integer and null for String?
Because that's the default, defined in the Java Language Specification.

Related

JavaFX: Storing null in a SimpleIntegerProperty

I have a SimpleIntegerProperty which should be able to store null. However, this is not possible, as written in the JavaDoc of IntegerProperty:
Note: setting or binding this property to a null value will set the property to "0.0". See setValue(java.lang.Number).
This also applies to other properties, such as LongProperty, FloatProperty, DoubleProperty, and BooleanProperty (but not to StringProperty, which allows null!). Why is this the case? Is there a workaround to store null in these properties?
The IntegerProperty.setValue(java.lang.Number) method is specified in the interfaces WriteableIntegerValue and WriteableValue. The JavaDoc of WriteableIntegerValue, states:
Note: this method should accept null without throwing an exception, setting "0" instead.
If you are looking at the code of the IntegerPropertyBase class, you can also see that the value is actually stored as a primitive int (which never can be null). This is also specified in the JavaFX API of SimpleIntegerProperty:
This class provides a full implementation of a Property wrapping a int value.
Solution:
You can simply circumvent that by using a SimpleObjectProperty<Integer> instead of a SimpleIntegerProperty, as a SimpleObjectProperty allows null values

How can is initialize all variables in a Java class at once to zero

I have some Java code with class variables and rather than set them to a particular value, I'd just like to initialize everything to zero. In C/C++, I can just do a memset() on the size of the structure, but how do I do the same thing in Java?
Primitives will default to zero. Objects will default to null. See https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html. However, as that article mentions, relying on the default value in Java is considered bad practice. The better practice is to set them all either when you declare them or in the constructor.
All primitive data types default to zero, only objects (and therefore Strings as well) default to null.
As long as you do not specify a value, all int, long, float, double etc. are 0 / 0.0 and all object and string variables are null.
Java initializes everything to 0 and null. But you should do this in constructor yourself.

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.

Use long or Long to map a "NUMBER" column in oracle?

I have a database column whose type is "NUMBER" in oracle.
I want to map it by long or Long. But I'm not sure which type I should use, the primitive type or the Object type? Is there a convention? My case is that the value is the only thing I want from DAO method:
public class SampleDAO{
public long fetchNumberValue(){
//Is it better to return long value instead of Long?
}
}
And what if I want to wrap that value in an object?
public class SampleObject(){
//Is it better to use Long to use methods like hashCode() inherited from Object?
private Long value;
}
EDIT:
Thanks all for the answers. I got it that it depends on whether I need accept null value.
This database column is not nullable, actually it's the primary key. And it may have such values like 0, 1, 2 etc... OK, another question appears, how can I know if I get a matched row with a 0 value or no matched row? java.sql.ResultSet.getLong returns a long value and if the column's value is SQL NULL then 0 is returned. Or is it a bad practice to have a 0 valid in such a column?
Long will permit a null value, where as long must not be null. So I would match it do your database column - is it nullable or not?
Edit to the edited question:
To check if a value was actually 0 or null in the database, use ResultSet.wasNull.
It comes down to if you want to accept nulls or not.
Usually, you want to use the primitive type, and not the object, if the only thing you want is the value, and that value won't be null. For all your conversion needs, you have the class (such as String.parse).
You should consider also that Long objects consume more memory -not sure in what amount- than long primitives. If the Nullable attribute is not a problem, you should use primitive data types.

How to check if a double is null?

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.

Categories