Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt on Local Arrays

 
meera kanekal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is regarding material from SCJP5 K&B in CH3 page 200 on the subject of Local Arrays.
Array elements are given default values regardless of whether they are declared as an instance or local variable. I understand that.
"The array object is itself not initialized if it is declared locally. In other words, you must explicitly initialize an array reference if it is declared and used within a method, but at the moment you construct an array object, all of its elements are assigned their default values". This is the part I am having difficulty with. Can anyone illustrate this with an example?
Thanks,
Meera
 
Prav sharma
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think like this



Here int_arr is the reference and the array object to which it is pointing will consist of 10 integer values.

What is told in the book is that all the ten integer values will be assigned there default values i.e 0
but you must explicitly initialise the reference to the array i.e. int_arr

I hope something is clear now.



meera kanekal wrote:This is regarding material from SCJP5 K&B in CH3 page 200 on the subject of Local Arrays.
Array elements are given default values regardless of whether they are declared as an instance or local variable. I understand that.
"The array object is itself not initialized if it is declared locally. In other words, you must explicitly initialize an array reference if it is declared and used within a method, but at the moment you construct an array object, all of its elements are assigned their default values". This is the part I am having difficulty with. Can anyone illustrate this with an example?
Thanks,
Meera
 
Sachin Adat
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prav sharma wrote:

I think you meant int[] int_arr = new int[10];
 
Prav sharma
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for correcting my mistake Sachin



Sachin Adat wrote:
Prav sharma wrote:

I think you meant int[] int_arr = new int[10];
 
Sachin Adat
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
meera kanekal wrote:This is the part I am having difficulty with. Can anyone illustrate this with an example?


intArray is initialized to null

intArray is an array of ints with 10 elements all initialized to 0

intArray is an array of int uninitialized

intArray is an array of ints with 10 elements all initialized to 0

Inference: Create an array object as instance variable or local variable using new, and you have your values of elements initialized!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic