Java – Display Array Elements

Java – Program to display array elements

  • Array consists more than one element.
  • It is recommended to process multiple elements using loops.
  • We use for() loop as we know the number of repetitions.
class Code
{
      public static void main(String[] args)
      {
                  int[] arr = {4, 2, 8, 9, 1, 6, 7, 5};
                  System.out.println(“Array elements are : “);
                  for (int i=0 ; i<=arr.length-1 ; i++)
                  {
                              System.out.println(arr[i]);
                  }
      }
}

Java – Program to display default values of array

  • As soon as memory allocated to array, all the locations initialized with default  values based on type of array
    • Int – 0
    • Double – 0.0
    • Char – blank
    • String or Object – null
    • Boolean – false
class Code
{
      public static void main(String[] args)
      {
                  int[] arr = new int[6];
                  System.out.println(“Default values of Array : “);
                  for (int i=0 ; i<=arr.length-1 ; i++)
                  {
                              System.out.println(arr[i]);
                  }
      }
}

Default Values of Object type:

class Code
{
      public static void main(String[] args)
      {
                  Object[] arr = new Object[6];
                  System.out.println(“Default values of Array : “);
                  for (int i=0 ; i<=arr.length-1 ; i++)
                  {
                              System.out.println(arr[i]);
                  }
      }
}
Scroll to Top