int Array Java


Apart char and string arrays, the most commonly used array is int Array in coding.

Following code on int Array shows the initialization and declaration of an integer array and printing its elements.

public class IntArray
{
  public static void main(String args[])
  {
    int marks[] = {40, 50, 60, 70, 80};    // int Array initialization

    int grades[] = new int[3];             // int Array creation or declaration
    grades[0] = 1;                         // assigning the elements after creation
    grades[1] = 2;
    grades[2] = 3;

    System.out.print("Printing the marks elements with old for loop : ");
    for(int i = 0; i < marks.length; i++)
    {
      System.out.print(marks[i] + " ");
    }

    System.out.print("\n\nPrinting the grades elements with Java 5 new for loop: ");
    for(int i : grades)
    {
      System.out.print(i + " ");
    }
  }
}

int Array

int marks[] = {40, 50, 60, 70, 80};

In the above statement, an int array is initialized. Initialization is nothing but declaration of array and assigning the values to elements goes at a time.

int grades[] = new int[3];
grades[0] = 1;
grades[1] = 2;
grades[2] = 3;

The other way of initialization is declaring array and giving the values each element. grades int array is created with new keyword with three elements. All the three are given values.

Know all the other array operations: All Array Operations at a Glance

Leave a Comment

Your email address will not be published.