Roger Jenkins wrote:If so, I would be interested to see an example how you can access these default values of an array that has been declared. If not, I would like to suggest an erratum.
Roger Jenkins wrote:Therefore, I would like to ask if it is really true that:
Array elements are always, always, always given their default values, regardless of where the array itself is declared or instantiated?
Roger Jenkins wrote:If so, I would be interested to see an example how you can PRINT OUT these default values of an array that has been declared. If not, I would like to suggest an erratum.
Roel De Nijs wrote:PS1. Because this is not an errata item, I removed your posts from the errata thread and turned them back into the original stand alone topic. Hope that's ok with you!
Roel De Nijs wrote:PS2. Always use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information. Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers. I've gone ahead and added the code tags for you. See how much easier the code is to read?
Roger Jenkins wrote:Thank you also for the extensive and well-thought out example you provided! I really do appreciate your effort in addressing my question!:-) Yes, what you say is true of course.
In any case, your example has clarified for me what is meant by the quoted sentence.
Roger Jenkins wrote:Look very carefully at the quoted sentence (in the K&B7, p. 188):
"Array elements are always, always, always given their default values, regardless of where the array itself is declared or instantiated."
You can read this sentence as follows (like I did):
It does not matter whether the array itself is declared, or whether the array itself is instantiated, and it also does not matter where this takes place, but the array elements are always, always, always given their default values.
Roger Jenkins wrote:In my humble opinion, the quoted sentence should therefore be changed into:
"Array elements are always, always, always given their default values, regardless of where the array itself is declared AND instantiated."
Roger Jenkins wrote:So maybe an erratum is warrented after all? But you decide. Either way is fine with me.
Paul Clapham wrote:If the array is declared but not instantiated, then there are no array elements (yet). So it's technically correct to say that all the array elements are assigned their default values, because there aren't any array elements, and therefore all of the zero array elements are assigned their default values.
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
Mike. J. Thompson wrote:But it is still logically true to say all of the array elements that are created are initialised to the default value.
Edit: as Paul said though, just because something is true it doesn't mean it is clear.
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
Array elements are always, always, always given default values, regardless where the array itself is declared or instantiated.
Junilu Lacar wrote:
As already understood: there's a difference between an array being initialized and the array elements being initialized.
So, regardless of whether an array is declared as a local variable or otherwise, when it is initialized, its elements are always assigned an initial value.
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
Paul Anilprem wrote:
Aren't you contradicting yourself?
Paul Anilprem wrote:
Further, I think you are mixing variable initialization and array initialization. blee is just a variable. This variable is being initialized to null. No array is being initialized here because there is no array in existence at this time! If there is no array, talking about its elements does not make any sense.
Junilu Lacar wrote:
For most intents, I usually just see the reference and the object as the same thing. If there's no object, the variable will be null.
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
Paul Anilprem wrote:Even if we ignore this distinction for a moment, I don't see how you can apply, "When instantiated, an array's elements are always assigned default values" to "String[] blee=null;" and assert that all the elements of blee are automatically initialized. If you consider the reference and the object as the same thing, then in the above line of code, blee is being initialized, it is not being instantiated.
Roger Jenkins wrote:Array elements are always, always, always given default values, regardless where the array itself is declared or instantiated
Array or its elements are always, always, always given default values, regardless where the array itself is declared or instantiated
Liutauras Vilda wrote:If quote would contain word "or":
Array or its elements are always, always, always given default values, regardless where the array itself is declared or instantiated
Then I would understand: if the array is declared, then is being given default value "null", or if array is instantiated is being give also default values to its elements 0's.
Roel wrote:But that statement is absolutely incorrect as an array declared as a local variable never gets a default value!
Liutauras Vilda wrote:I was looking at OP's provided code example. But you're right, then words "always" x 3 are incorrect. I give up
Junilu Lacar wrote:
However, when blee gets assigned an array, such as when this statement is executed
blee = new String[2];
blee can be treated as if it were the actual object. So at this point, blee is an array and all its elements are, in fact, initialized to null.
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
Paul Anilprem wrote:
The statement, "Array elements are always, always, always given default values, regardless where the array itself isdeclared orinstantiated." would be better.
Paul Anilprem wrote:
Just to be clear, I do not agree with your point of coalescing the variable and object. I'm just saying even with that assumption the statement is incorrect.
Roger Jenkins wrote:"The elements of an array are only given their default values after instantiation of an array. If the array is merely declared, the array will get the value null. Trying to print out the array elements after merely the declaration of an array will lead to a NullPointerException. However, if an array is declared and instantiated, it does not matter where this declaration and instantiation takes place."
Roel De Nijs wrote:
Roger Jenkins wrote:"The elements of an array are only given their default values after instantiation of an array. If the array is merely declared, the array will get the value null. Trying to print out the array elements after merely the declaration of an array will lead to a NullPointerException. However, if an array is declared and instantiated, it does not matter where this declaration and instantiation takes place."
This part of your statement is incorrect: If the array is merely declared, the array will get the value null. Trying to print out the array elements after merely the declaration of an array will lead to a NullPointerException.
That's only true as your array is declared as a class or instance variable; if the array is declared as a local variable, it never gets a default value. And if you try to print out the array elements of a local array variable which is merely declared, you'll get a compiler error; not a NullPointerException.
Paul Anilprem wrote:Do you mean you see no difference between:
and
and that in both the cases all the array elements are assigned their default value?
Roger Jenkins wrote:Array elements are always, always, always given default values, regardless where the array itself is declared or instantiated."
.
Paul Anilprem wrote:The statement, "Array elements are always, always, always given default values, regardless where the array itself is
declared orinstantiated." would be better.
Campbell Ritchie wrote:The exception being when an initialiser is used.
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
Campbell Ritchie wrote:The exception being when an initialiser is used.
Campbell Ritchie wrote:That is the one instance when you can instantiate an array whose elements do not point to their default values.
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
It's exactly the same and completely different as this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|