This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array Declaration

 
Arijit Ghosh
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I may sound a bit like a broken record, but ... have you tried compiling and running a program with each of these alternatives?
 
Arijit Ghosh
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Local variables in methods are NOT initialized by default.
 
Arijit Ghosh
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Arijit Ghosh
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic