What happens if we create array but not initialize

Java un-initializing Array

We can create and initialize array using different statements, but what will be the content of the arrays if we cont initialize the array after creating it?
Let's create arrays of different data types and then print the content of each array to see what they contain.
Create Different Arrays
Create different arrays of primitive data types and one Object type (Integer)
byte rollNums1[] = new byte[10];
short rollNums2[] = new short[10];
int rollNums3[] = new int[10];
long rollNums4[] = new long[10];
float rollNums5[] = new float[10];
double rollNums6[] = new double[10];
char rollNums7[] = new char[10];
boolean rollNums8[] = new boolean[10];
Integer rollNums9[] = new Integer[10];
Iterate the arrays and print the content.
    byte rollNums1[] = new byte[10];
    System.out.println("\nbyte array");
    for (byte i : rollNums1) {
      System.out.print(i + ", ");
    }
    short rollNums2[] = new short[10];
    System.out.println("\nshort array");
    for (short i : rollNums2) {
      System.out.print(i + ", ");
    }
    
    int rollNums3[] = new int[10];
    System.out.println("\nint array");
    for (int i : rollNums3) {
      System.out.print(i + ", ");
    }
    
    long rollNums4[] = new long[10];
    System.out.println("\nlong array");
    for (long i : rollNums4) {
      System.out.print(i + ", ");
    }
    
    float rollNums5[] = new float[10];
    System.out.println("\nfloat array");
    for (float i : rollNums5) {
      System.out.print(i + ", ");
    }
    
    double rollNums6[] = new double[10];
    System.out.println("\ndouble array");
    for (double i : rollNums6) {
      System.out.print(i + ", ");
    }
    
    
    char rollNums7[] = new char[10];
    System.out.println("\nchar array");
    for (char i : rollNums7) {
      System.out.print(i + ", ");
    }
    
    boolean rollNums8[] = new boolean[10];
    System.out.println("\nboolean array");
    for (boolean i : rollNums8) {
      System.out.print(i + ", ");
    }
    
    Integer rollNums9[] = new Integer[10];
    System.out.println("\nInteger array");
    for (Integer i : rollNums9) {
      System.out.print(i + ", ");
    }
Output
byte array 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
short array 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
int array 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
long array 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
float array 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
double array 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 
char array , , , , , , , , , , 
boolean array false, false, false, false, false, false, false, false, false, false, 
Integer array null, null, null, null, null, null, null, null, null, null,

Conclusion
  • If the array is just created and not initialized with data then the content of the arrays depends on the data type.
  • For byte, short, int, long it is 0
  • For float and double it is 0.0
  • for char it is '' (empty character)
  • for boolean it is false
  • for Integer or any other object, it is null
  • The value of the array depends on the type of the array and a default value for the respective data types.

References

No comments :

Post a Comment

Please leave your message queries or suggetions.