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.
Related
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.
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.
This question already has answers here:
What is the default initialization of an array in Java?
(8 answers)
Closed 9 years ago.
I have two-dimensional array
protected MyClass[][] myArray;
in constructor I have this
this.myArray= new MyClass[20][20];
Now, without inicialization (aka this.myArray[2][2] = new MyClass(par0, par1);)
the value of this.myArray[2][2] is "null".
Is this guaranteed? And where can I read more about this subject? (for primitive types like int or boolean too)
Thanks
Yes, it's guaranteed. Array values are initialized with null (for objects), 0 (for numeric primitives) and false (for boolean primitives), just like fields.
See http://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html#jls-10.6-100:
Space is allocated for a new array of that length. If there is insufficient space to allocate the array, evaluation of the array initializer completes abruptly by throwing an OutOfMemoryError. Otherwise, a one-dimensional array is created of the specified length, and each component of the array is initialized to its default value (ยง4.12.5).
(emphasis mine)
Yes, it is guaranteed. Every type has a default initialization value:
numeric primitives = 0
boolean = false
all Objects = null
Yes. This behavior is guaranteed. The default value of an Object is null. Therefore the default values for an array of Objects is also null so every element in the array needs to be instantiated. See Default Values in Data Types.
I have a program that creates lists and needs any assigned values to be 0. Its been running fine when I do with int[] humpty_dumpty = new int[20]; but to optimize the size of the lists I set them to Short[] and now my program is breaking because it takes the zero's as inputs(and Short[] humpty_dumpty = new Short[20]; is making the default value null).
Is there a way to set it to default zero without having to iterate through the entire list(I can do this via a for loop but was wondering if there was a way to make its behavior similar to int)?
There is a difference between a Short[] and a short[]. Elements of the latter will be initialized to 0 because short is a "primitive" type and cannot be null. The capitalized Short class will be initialized to null because it is really just an Object wrapping a short value.
You may create an array of primitive type instead of wrapper,
short []ar=new short[20];
Short[] doesn't 'optimize the size of the lists' at all, and it has a default value of null. short[] does, and it has a default value of zero.
Answers, in order:
1) Requirement first, optimization last. Don't use sparse arrays, or try to be 'smart', unless you specifically need to do this & deal with the extra code/ overhead.
2) Use common methods (possibly in an Instance or Static Helper class) to avoid repeating common code.
eg. short sparseGet (Short[] array, int i) {return (array[i] != null ? array[i] : 0);}
3) Perhaps use short[] rather than Short[]? Uppercase types are not primitives, but Value Wrapper classes -- and stored as object references (pointers) to instances, thus slower & more memory-intensive.
4) Uppercase 'Value Wrapper' types are appropriate where you may have null values, from a database. eg. Person.Age would ideally be Integer, if there's any possibility you/ the database might not have data for that field.
Cheers.
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.