If we don’t assign values to array elements and try to access them, the compiler does not produce an error as in the case of simple variables. Instead, it assigns values that aren’t garbage.
Below are the default assigned values.
| S. No. |
Datatype |
Default Value |
| 1 |
boolean |
false |
| 2 |
int |
0 |
| 3 |
double |
0.0 |
| 4 |
String |
null |
| 5 |
User-Defined Type |
null |
Example:
Java
class ArrayDemo {
public static void main(String[] args)
{
System.out.println("String array default values:");
String str[] = new String[5];
for (String s : str)
System.out.print(s + " ");
System.out.println(
"\n\nInteger array default values:");
int num[] = new int[5];
for (int val : num)
System.out.print(val + " ");
System.out.println(
"\n\nDouble array default values:");
double dnum[] = new double[5];
for (double val : dnum)
System.out.print(val + " ");
System.out.println(
"\n\nBoolean array default values:");
boolean bnum[] = new boolean[5];
for (boolean val : bnum)
System.out.print(val + " ");
System.out.println(
"\n\nReference Array default values:");
ArrayDemo ademo[] = new ArrayDemo[5];
for (ArrayDemo val : ademo)
System.out.print(val + " ");
}
}
|
Output
String array default values:
null null null null null
Integer array default values:
0 0 0 0 0
Double array default values:
0.0 0.0 0.0 0.0 0.0
Boolean array default values:
false false false false false
Reference Array default values:
null null null null null
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!
Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!
Last Updated :
24 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...