• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array elements are always given default values when declared? (K&B7, page 188)

 
Roger Jenkins
Greenhorn
Posts: 22
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On page 188 of OCA/OCP Java SE 7 Programmer I & II Study Guide (K&B7), the middle of the page has the following sentence:
"The bottom line: Array elements are always, always, always given default values, regardless where the array itself is declared or instantiated."

This does not seem to be true (as far as I can see at least).

Consider the following code:



This gives a NullPointerException.
In principle, I can understand why: An array is an object, and every object that has been declared but not initialized will get a value of null. If you try to do something with null, you then get a NullPointerException. This seems to be the case here.

HOWEVER, in the example above, the array itself has been declared (just like the sentence on page 188 in K&B suggests) and there seems no way to access these ELEMENTS (which are supposedly being given their default values, in other words: 0).
The fact that the sentence in the book which I have quoted is italicized for emphasis, and also uses the word "always" 3 times, makes me think I am overlooking something. Otherwise, this kind of emphasis seems to be ironic. In any case, I cannot see how you can access the default values of an array that is declared.
The example that the book itself uses in this context directly below the quoted sentence, seems to construct/instatantiate the array, and also to initialize it, so therefore it seems not to be representative for the

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?

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
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It was my intention to post my Post in the section:
http://www.coderanch.com/t/641206/ocajp/certification/Errata-OCA-OCP-Java-SE

instead of creating a stand alone topic.

I think I should have pushed the button "Post reply" instead of the button "New topic".
 
Roger Jenkins
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


The last two sentences in my Post cotained a poor choice of words, when I said "access these default values". My last two sentences should been better formulated as follows:

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.
 
Paul Clapham
Sheriff
Posts: 21559
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to say I'm not keen on the word "declared" there because to me it refers to the code you posted, too. For the "instantiated" part consider this modification:



and you'll see how that array is filled with all zeroes as part of the instantiation. But hopefully the regulars who know more about certification exams will drop in with their opinions.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Yes, that statement is absolutely true. But you must read it very carefully. The statement clearly states that array elements always, always, always get their default values. And an array element is not the same as an array.

In your example, you declared a class variable year which is a reference variable to an int array. You didn't instantiate/initialize the array, so because it's a class variable, it gets a default value (as you probably know). The default value for a reference variable is null. And you try to access the 2nd element (at index 1) of a null array and that's why you'll get a NullPointerException.

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.

If you would change your program accordingly and instantiate this array, you'll see that array elements are always, always, always given their default values, regardless if this array is defined/declared as a class variable, instance variable or local variable. Illustrated in this code exampleOutput:
years: [0, 0, 0, 0, 0]
objects: [null, null, null, null, null, null, null, null, null, null]
floats: [0.0, 0.0]
ints[0]: [0, 0]
bools: [false, false, false]
strings: [null, null]
ints[1]: [0, 0]
bools: [false, false, false]
strings: [null, null]


So regardless where the array was declared and/or instantiated, the array elements always, always, always get their default values.

Hope it helps!
Kind regards,
Roel

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!
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
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!

Yes, that is certainly ok with me! I just wanted to have attention for this topic as an potential errata item (and therefore initially wanted to post it as such). Having said that, I'm not sure yet that it doesn't belong there. But whether this topic actually makes the errata list in the end, I'll gladly leave that decision up to 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?

Yes, the code is indeed much easier to read. Thank you for giving me the link and showing me how to do this!

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.

However, I was aware (as I have stated) that my example would lead to a NullPointerException. The reason I used my example was to falsify the quoted sentence. And I still stand by it.

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.

In other words, the quoted sentence suggests that if the array itself is merely declared (and not necessarily instantiated), the array elements are still always given their default values.
That is how I at least read the sentence.

My example, in which I merely declared the array, illustrated that what the sentence says is not true (or at least what I think the sentence says isn't true).
(It is logical that you cannot print out an element if an array is not instantiated because you don't even know how many elements there are).

In your example, you don't merely declare an array, but you actually instantiate the arrays.
That is why you can print out the array elements. Otherwise you also would logically have a NullPointerException.

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."

So maybe an erratum is warrented after all? But you decide. Either way is fine with me.

In any case, thanks to your example, I know now what is being meant and I can go on. So thank you!
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

You are welcome! Thanks for these kind words. Highly appreciated!

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.

Now I understand better what's confusing you. It's about the interpretation of that statement. And when it's sheer about the interpretation of a statement, I'm always very careful about adding it to the errata overview. Simply because how you interpret a statement can be very personal. And this statement is a very good example as my interpretation is totally different (as shown in my previous post ). The errata thread was created almost one year ago and as far as I know (remember) you are the first one to report this statement. If several different readers (10+) would have reported this statement as confusing, it was probably already been on the errata overview.

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."

First of all, I think the authors just want to emphasize the fact that it doesn't matter where the array itself is declared or instantiated/initialized. And that's why they used "or". So you could declare an array as a class variable and instantiate/initialize it in a switch statement in a local private method; it doesn't matter at all: the array elements will get a default value.
Secondly I think prior statements in this paragraph clearly explain that an array which is only declared but not instantiated/initialized, gets a default value of null (just like any other object reference instance variable). So an only-declared array refers to null, so that means there are no array elements. And if no array elements exist, they definitely can't be assigned a default value. So that should help by correctly interpreting that statement.
Finally I think there could be a chance that readers will wrongly interpret your suggested fix for that statement as well. By stating "the array itself is declared AND instantiated", people might think there is a difference in behavior (with regard to default values for array elements) between an array which is declared and instantiatedand an array which is declared and instantiated later on in the code

Roger Jenkins wrote:So maybe an erratum is warrented after all? But you decide. Either way is fine with me.

Currently this will not be added to the errata overview (for the aforementioned reasons). But if other readers chime in and report this statement as being confusing, it might still make it to the errata overview

Hope it helps!
Kind regards,
Roel
 
Paul Clapham
Sheriff
Posts: 21559
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

But I can tell you from personal experience that students in a second-year university math class have difficulty comprehending the idea that you can make a meaningful statement about the elements of the empty set (which is what I just did there), and also the idea that every statement you make about the elements of the empty set is true. So in the previous paragraph, if the array is declared but not instantiated then it's also true that each of the array elements is written to System.out -- this is true because there aren't any array elements.

So, yeah. The statement being discussed here is correct. But from what I wrote there you can see how it could be confusing, and it might be an idea to reformulate it even if it isn't wrong.
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3817
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Do you mean you see no difference between:

and

and that in both the cases all the array elements are assigned their default value?
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well there is obviously a functional difference between the two statements so you can't say they are equivalent. But it is still logically true to say all of the array elements that are created are initialised to the default value.

Can you show which array element wasn't initialised to the default value?

Edit: as Paul said though, just because something is true it doesn't mean it is clear.
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3817
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Forget clear. It is technically incorrect.
It is one thing to have a zero balance in an account. It is another to not have an account
A null is fundamentally different from an array of 0 elements. I do not agree that the empty set logic applies here. An empty set is not same as no set.
 
Junilu Lacar
Bartender
Pie
Posts: 8785
81
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Array elements are always, always, always given default values, regardless where the array itself is declared or instantiated.

The fact that there are at least two interpretations shows that this statement is ambiguous. This is ambiguous because of a failure to separate concerns.

Let me break this down:

- The two concerns are declaration and instantiation
- There's doubt as to whether the statement should say "declared or instantiated" or "declared and instantiated"

Here's what we know about declarations and how it relates to initialization to default values:
- Class and instance variables are always initialized with default values
- Local variables are not initialized with default values; they must be explicitly initialized

This is the reason for saying "where": it's referring to the difference between being declared inside or outside a method.

Here's what we know about instantiation:
- When instantiated, an object's instance variables are always assigned default values
- When instantiated, an array's elements are always assigned default values

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.

In code:

(Edited to address Paul Aniprem's point)
 
Junilu Lacar
Bartender
Pie
Posts: 8785
81
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose you could also look at this this way, which is far less confusing, IMO:

Objects will always have their instance variables implicitly initialized to default values if not explicitly initialized.

Arrays are objects. If you think of an array's elements as its instance variables, then it follows that its elements are always initialized to default values if not explicitly initialized.
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3817
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Aren't you contradicting yourself? In the first statement you acknowledge the difference between an array being initialized and the array elements being initialized.
In your code, you write:



Your second statement quoted above implies that you believe blee is initialized and its elements are also initialized. That is contradictory to the first statement.

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
Bartender
Pie
Posts: 8785
81
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Anilprem wrote:
Aren't you contradicting yourself?

Depends on whether you're bringing further confusion by differentiating between the array object itself and a reference to it. When you do that everything becomes quite a mouthful. Instead of just saying "the array" we now have to say "an array reference" vs "the array object itself"

The distinction you're making is that technically, it's the array reference that is implicitly initialized to null. There is no array object being referenced.

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.

For most intents, I usually just see the reference and the object as the same thing. If there's no object, the variable reference will be null. I feel that making further distinction in this case becomes borderline pedantic.
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3817
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

That is fine but only until it leads to the absurd conclusion that all the elements of an array that doesn't exist are always automatically initialized.

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.
 
Junilu Lacar
Bartender
Pie
Posts: 8785
81
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Not sure if it's you or me but to me

String[] blee = null;

means that blee does not reference an array object. null means that there's no array so at this point, you're right, blee is just a reference variable.

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.

-----
You know, this feels almost the same as though I'm arguing the difference between my name and my "self". For most intents, I am Junilu. Yes, "Junilu" is my name and technically, I am NOT my name. In the current context, however, this line of reasoning becomes absurd, quite frankly.
 
Liutauras Vilda
Bartender
Pie
Posts: 2785
112
BSD VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roger Jenkins wrote:Array elements are always, always, always given default values, regardless where the array itself is declared or instantiated

Back to original OP quote I understand also, that after this statement:
Based on the quote, their elements are given default values, but indeed it is not true as elements doesn't exist as we even don't know how many elements in it. I'd say this quote is misleading.

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 De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

But that statement is absolutely incorrect as an array declared as a local variable never gets a default value (not even null)!
 
Liutauras Vilda
Bartender
Pie
Posts: 2785
112
BSD VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel wrote:But that statement is absolutely incorrect as an array declared as a local variable never gets a default value!

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
Bartender
Pie
Posts: 8785
81
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

Aww, c'mon now, Liutauras, it's not that bad.

Obviously, none of us have a Ph.D. in Linguistics but nitpicking on semantics aside, I think there's enough here to clarify the idea, if not the phrasing of it.
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3817
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

At this time, Yes. But not at the time when all you have is blee = null.

Therefore, the statement, "Array elements are always, always, always given default values, regardless where the array itself is declared or instantiated." is incorrect.

The statement, "Array elements are always, always, always given default values, regardless where the array itself is declared or instantiated." would be better.

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.
 
Junilu Lacar
Bartender
Pie
Posts: 8785
81
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Anilprem wrote:
The statement, "Array elements are always, always, always given default values, regardless where the array itself is declared or instantiated." would be better.

I can buy that. That's in line with what I said about confusion arising from mixing the implications of where it is declared vs where it is instantiated. Removing "declared" eliminates the mixing.

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
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For myself, to explain and clarify the sentence "The bottom line: Array elements are always, always, always given default values, regardless where the array itself is declared or instantiated." (in K&B, page 188)
I have left the sentence intact, but I have now also added the following to it with pencil in my copy of the book:

"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."

Thank you all for your comments!
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Roger Jenkins
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Thank you for the correction! That is indeed correct.
Therefore, I'll also add what you just said with pencil into my book next to the rest of my earlier clarification for the quoted sentence in K&B7!
 
Paul Clapham
Sheriff
Posts: 21559
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Anilprem wrote:Do you mean you see no difference between:

and


No, clearly the two are different.

and that in both the cases all the array elements are assigned their default value?


In the first case there is no array, and hence no array elements. In the second case there is an array, but it has zero elements, so again there are no array elements. In both cases, all of the array elements are assigned their default value because there aren't any of them. Not only that, but all of the array elements are e-mailed to a secret address in North Korea. Or anything else you like to say about all of the array elements, because there aren't any of them.

Since this post there has been a long list of other posts and it seems to me that we can now agree that talking about array elements in the context of declaring an array variable is confusing and should be reconsidered.
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roger Jenkins wrote:Array elements are always, always, always given default values, regardless where the array itself is declared or instantiated."

.

Your problem is just your array has no elements...
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Anilprem wrote:The statement, "Array elements are always, always, always given default values, regardless where the array itself is declared or instantiated." would be better.

Based on this great discussion, I added this suggestion to the errata overview. It's now in the hands (and minds) of the authors to decide if they'll change the statement in the next version of the study guide.
 
Campbell Ritchie
Sheriff
Posts: 51368
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The exception being when an initialiser is used.
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3817
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:The exception being when an initialiser is used.

This is an exception to what?
 
Campbell Ritchie
Sheriff
Posts: 51368
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is the one instance when you can instantiate an array whose elements do not point to their default values.
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:The exception being when an initialiser is used.


The very sentence affter the "always, always always" sentence is "If we initialize an array, object reference elements will equal null if they are not initialized individually with values. " Seriously I don't think anything is unclear when you read the whole paragraph. In fact it also covers the case where it's declared and not instantiated.
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3817
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:That is the one instance when you can instantiate an array whose elements do not point to their default values.

Good point
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic