This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Arrays 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 » Java » Beginning Java
Bookmark "Arrays" Watch "Arrays" New topic
Author

Arrays

geetha nagarajan
Ranch Hand

Joined: Jul 13, 2001
Posts: 94
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

Joined: Dec 20, 2001
Posts: 3271
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


SCJP Tipline, etc.
Dave Wingate
Ranch Hand

Joined: Mar 26, 2002
Posts: 262
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"); }


Fun programming etcetera!
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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...


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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

Joined: Dec 20, 2001
Posts: 3271
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

Joined: Jul 13, 2001
Posts: 94
Thanks a lot,got the concept clearly now.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Arrays
 
Similar Threads
Battleships - guys please help me out!
Array Initilization
arraycopy() with String[][]
loops
Two Dimensional Arrays