File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt on Local Arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt on Local Arrays" Watch "Doubt on Local Arrays" New topic
Author

Doubt on Local Arrays

meera kanekal
Ranch Hand

Joined: Feb 13, 2005
Posts: 75
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

Joined: Feb 07, 2005
Posts: 102
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

Joined: Sep 03, 2007
Posts: 213
Prav sharma wrote:

I think you meant int[] int_arr = new int[10];


SCJP 6
How To Ask Questions On Java Ranch - How To Answer Questions On Java Ranch
Prav sharma
Ranch Hand

Joined: Feb 07, 2005
Posts: 102
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

Joined: Sep 03, 2007
Posts: 213
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!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt on Local Arrays