Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Arrays

 
geetha nagarajan
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int a[] = new int[2];
for(int j =0;j<a.length;j++)
{
System.out.println(a[j]);
}
char c[] = new char[2];
for(int i=0;i<c.length;i++)
{
System.out.println(c[i]);
}
The int array is intialised to default value '0' & prints it as output.
The char array is not initialised to default values & thereby prints nothing.
Pl.explain ,why?
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by geetha nagarajan:
The char array is not initialised to default values & thereby prints nothing.

Not true. When an array is created, all contents of that array are initialized to their default values. For any primitive, this value is 0.
Therefore, when you print out the contents of the int array, you see a couple zeros.
However, the character represented by 0 is not a viewable character. (See here) Therefore, you don't see anything printed out. It's not that the values aren't getting printed, I'm sure they are. In fact, to prove it, cast the char value to an int before printing it, like this:

You should see that zeros are being printed now for the char array, as well.
I hope that helps clear that up for you.
Corey
 
Dave Wingate
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The char array is not initialised to default values

Every array, regardless of type, is initialised to default values.
To demonstrate that your char array is, in fact, initialised to default values, you could add the following to the end of your code:
if (c[0] != null) { System.out.println("I was initialised"); }
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by geetha nagarajan:
The char array is not initialised to default values & thereby prints nothing.
Pl.explain ,why?

Doch! Components of all arrays are initialized when a new array is allocated in memory. The components of a char array are initialized to zero. Consider:

This prints two zeros - the components of the char array were indeed initialized.
In your initial experiment, you were displaying Character(0) - which is...
Oh, just a second, I'll find it - it may be a space...
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, yes. The old three-fer.
Well, it looks like Corey found the information regarding Charater(0). And just to nit pick a bit...
There is an exception to this:
Originally posted by Corey McGlone:
When an array is created, all contents of that array are initialized to their default values. For any primitive, this value is 0.

boolean is a primitive data type and the default value for a boolean is false - not 0.
Good Luck.
[ April 02, 2002: Message edited by: Dirk Schreckmann ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dirk Schreckmann:
boolean is a primitive data type and the default value for a boolean is false - not 0.

Bah! There's my C++ background coming back to haunt me. Unlike in C++, 0 does not represent false in Java. Dirk's right, booleans are initialized to false, not 0. Thanks, Dirk.
Corey
 
geetha nagarajan
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot,got the concept clearly now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic