Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Array Declaration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Array Declaration" Watch "Array Declaration" New topic
Author

Array Declaration

Arijit Ghosh
Ranch Hand

Joined: Feb 01, 2002
Posts: 174
A very simple and elementary doubt, but I guess very important for the Certification--
1. int[] i;
2. int[] i = new int[3];
3. Car[] i;
4. Car[] i = new Car[3];
System.out.println(i[0]);
What will be the output in each case ?
In which one is the Array DEFINED ?
In which one is the Array DECLARED ?
In which one is the Array INITILAIZED ?


Regards,<br /> Arijit
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
I may sound a bit like a broken record, but ... have you tried compiling and running a program with each of these alternatives?


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Arijit Ghosh
Ranch Hand

Joined: Feb 01, 2002
Posts: 174
Yes, I have and find that in case 1 and 3, compilation error is shown....
While in the other 2 cases, the default value is PRINTED.
In case 2, it is 0 and in case 4 it is null, as it should be.
I read that whether an array is declared or initialized, in both the cases, the array elements are initialized to default values.
So assuming Case 1 and 3 are DECLARATION cases, so when I try to print the element values, it should show the default values as in case 2 and 4.
Am I missing something ?
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Local variables in methods are NOT initialized by default.
Arijit Ghosh
Ranch Hand

Joined: Feb 01, 2002
Posts: 174
What happens if it is not local variable but is a member variable ? I can create a non-static member variable and then try to print an element of that array as shown...


It gives Null Pointer exception... Why ?
[ November 25, 2002: Message edited by: Arijit Ghosh ]
Anup Katariya
Greenhorn

Joined: Oct 23, 2002
Posts: 29
if u write your member variable as
int j[] = new int[3];
then it wont give any errors.
You are confused between array reference and array elements.


SCJP
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
int[] j; // is initialized to a null reference
Unless and until you assign something to j, any reference to j[0] will cause a NullPointerException.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Arijit, this is the problem:

I read that whether an array is declared or initialized, in both the cases, the array elements are initialized to default values.

The variable declared as an array follows the same path regarding intialization as any reference variable. It would be initialized to null if it was declared as a field of the object. Being a local variable it will not be initialized by its mere declaration. However the elements of the array are another story. Whenever the array is created, either as a member or local to a method, its elements will be initialized to its corresponding default value.


SCJP2. Please Indent your code using UBB Code
Arijit Ghosh
Ranch Hand

Joined: Feb 01, 2002
Posts: 174
Thanks everyone...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array Declaration
 
Similar Threads
what It means if you don't override equals()
"empty" array declarations
Accessing Arrays and Editing Objects Within Them From Other Class'
Car Inventory Problems
How to initialize array bidimensional ?