Download Java int Array in Formats
QR Code for this Page
Java int Array
Complete Tutorial on Java primitive int data type array.

Java int Array

int Array in Java

Java int array is used to store int data type values only. The default value of the elements in a int array is false.

With the following Java int array examples you can learn

  • how to declare Java int array
  • how to assign values to Java int array
  • how to get values from Java int array

Initializing a int Array in Java

Arrays are declared with [] (square brackets). If you put [] (square brackets) after any variable of any type only that variable is of type array remaining variables in that declaration are not array variables those are normal variables of that type.

If you put [] (square brackets) after any data type all the variables in that declaration are array variables. All the elements in the array are accessed with index. The array element index is starting from 0 to n-1 number i.e. if the array has 5 elements then starting index is 0 and ending index is 4.

// declares an array of ints
int[] intArray;

What is the Length of an Array in Java ?

In Java all the arrays are indexed and declared by int only. That is the size of an array must be specified by an int value and not long or short. All the arrays index beginning from 0 to ends at 2147483646. You can store elements upto 2147483647. If you try to store long (big) elements in array, you will get performance problems. If you overcome performance problems you should go to java collections framework or simply use Vector.

Syntax :
1
2
< data type > < variable >[];
< data type >[] < variable >;

Java int Array Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*  Java int Array Example
    Save with file name IntArray.java */
  
public class IntArray
{
  public static void main(String args[])
  {
    //JAVA INT ARRAY DECLARATION
    int n[];
    //MEMORY ALLOCATION FOR JAVA INT ARRAY
    n = new int[4];
    //ASSIGNING ELEMENTS TO JAVA INT ARRAY
    n[0] = 5;
    n[1] = 3;
    n[2] = 4;
    n[3] = 7;
    //JAVA INT ARRAY OUTPUT
    System.out.println("Java int Array Example");
    for(int i=0;i<n.length;i++)
    {
      System.out.println("Element at Index : "+ i + " " + n[i]);
    }
  }
}

Following Java int array example you can learn how to assign values to java int array at the time of declaration.

How to assign values to java int array at the time of declaration

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*  How to assign values to java int array at the time of declaration Example
    Save with file name IntArray2.java  */
  
public class IntArray2
{
  public static void main(String args[])
  {
    //JAVA INT ARRAY DECLARATION
    int n[] = {5,3,4,7};
    //INT ARRAY OUTPUT
    System.out.println("Java int Array Example");
    for(int i=0;i<n.length;i++)
    {
      System.out.println("Element at Index : "+ i + " " + n[i]);
    }
  }
}

Following Java int array example you can learn how to declare Java int array with other Java int array variables.

How to declare Java int array with other Java int array variables

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*  How to declare Java int array with other Java int array variables Example
    Save with file name IntArray3.java  */
  
public class IntArray3
{
  public static void main(String args[])
  {
    //JAVA INT ARRAY DECLARATION
    //n IS AN ARRAY n2 IS NOT AN ARRAY
    int n[], n2;
    //MEMORY ALLOCATION FOR JAVA INT ARRAY
    n = new int[4];
    //ASSIGNING ELEMENTS TO JAVA INT ARRAY
    n[0] = 5;
    n[1] = 3;
    n[2] = 4;
    n[3] = 7;
    n2 = 555;
    //JAVA INT ARRAY OUTPUT
    System.out.println("Java int Array Example");
    System.out.println("n2 value is : "+n2);
    for(int i=0;i<n.length;i++)
    {
      System.out.println("Element at Index : "+ i + " " + n[i]);
    }
  }
}

Following Java int array example you can learn how to assign Java int array to other Java int array.

How to assign Java int array to other Java int array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*  How to assign Java int array to other Java int array Example
    Save with file name IntArray4.java  */
  
public class IntArray4
{
  public static void main(String args[])
  {
    //JAVA INT ARRAY DECLARATION
    //n AND n2 ARE ARRAY VARIABLES
    int[] n, n2;
    //MEMORY ALLOCATION FOR JAVA INT ARRAY
    n = new int[4];
    //ASSIGNING ELEMENTS TO JAVA INT ARRAY
    n[0] = 5;
    n[1] = 3;
    n[2] = 4;
    n[3] = 7;
    //ASSIGNING n ARRAY TO n2 ARRAY VARIABLE
    n2 = n;
    //JAVA INT ARRAY OUTPUT
    System.out.println("Java int Array Example");
    System.out.println("n array values");
    for(int i=0;i<n.length;i++)
    {
      System.out.println("Element at Index : "+ i + " " + n[i]);
    }
    System.out.println("n2 array values");
    for(int i=0;i<n2.length;i++)
    {
      System.out.println("Element at Index : "+ i + " " + n2[i]);
    }
  }
}
Download Java int Array in Formats