Java int Array

It is used to store int data type values only. With the following examples you can learn how to declare int array, how to assign values to int array and how to get values from int array.

Java int Array Example
/* int Array Example */
/* Save with file name IntArray.java */

public class IntArray
{
	public static void main(String args[])
	{
		//INT ARRAY DECLARATION
		int n[];
		//MEMORY ALLOCATION FOR INT ARRAY
		n = new int[4];
		//ASSIGNING ELEMENTS TO INT ARRAY
		n[0] = 5;
		n[1] = 3;
		n[2] = 4;
		n[3] = 7;
		//INT ARRAY OUTPUT
		System.out.println();
		System.out.println("int Array Example");
		System.out.println("==================");
		System.out.println();
		for(int i=0;i<n.length;i++)
		{
			System.out.println("Element at Index : "+ i + " " + n[i]);
		}
	}
}
                        

In this example you can learn how to assign values to int array at the time of declaration.

Java int Array Example 2
/* int Array Example 2 */
/* Save with file name IntArray2.java */

public class IntArray2
{
	public static void main(String args[])
	{
		//INT ARRAY DECLARATION
		int n[] = {5,3,4,7};
		//INT ARRAY OUTPUT
		System.out.println();
		System.out.println("int Array Example");
		System.out.println("==================");
		System.out.println();
		for(int i=0;i<n.length;i++)
		{
			System.out.println("Element at Index : "+ i + " " + n[i]);
		}
	}
}
                        

In this example you can learn how to declare int array with other int variables.

Java int Array Example 3
/* int Array Example 3 */
/* Save with file name IntArray3.java */

public class IntArray3
{
	public static void main(String args[])
	{
		//INT ARRAY DECLARATION
		int n[], n2; //n IS AN ARRAY n2 IS NOT AN ARRAY
		//MEMORY ALLOCATION FOR INT ARRAY
		n = new int[4];
		//ASSIGNING ELEMENTS TO INT ARRAY
		n[0] = 5;
		n[1] = 3;
		n[2] = 4;
		n[3] = 7;
		n2 = 555;
		//INT ARRAY OUTPUT
		System.out.println();
		System.out.println("int Array Example");
		System.out.println("==================");
		System.out.println();
		System.out.println("n2 value is : "+n2);
		System.out.println();
		for(int i=0;i<n.length;i++)
		{
			System.out.println("Element at Index : "+ i + " " + n[i]);
		}
	}
}
                        

In this example you can learn how to assign int array to other int array.

Java int Array Example 4
/* int Array Example 4 */
/* Save with file name IntArray4.java */

public class IntArray4
{
	public static void main(String args[])
	{
		//INT ARRAY DECLARATION
		int[] n, n2; //n AND n2 ARE ARRAY VARIABLES
		//MEMORY ALLOCATION FOR INT ARRAY
		n = new int[4];
		//ASSIGNING ELEMENTS TO INT ARRAY
		n[0] = 5;
		n[1] = 3;
		n[2] = 4;
		n[3] = 7;
		//ASSIGNING n ARRAY TO n2 ARRAY VARIABLE
		n2 = n;
		//INT ARRAY OUTPUT
		System.out.println();
		System.out.println("int Array Example");
		System.out.println("==================");
		System.out.println();
		System.out.println("n array values");
		System.out.println();
		for(int i=0;i<n.length;i++)
		{
			System.out.println("Element at Index : "+ i + " " + n[i]);
		}
		System.out.println();
		System.out.println("n2 array values");
		for(int i=0;i<n2.length;i++)
		{
			System.out.println("Element at Index : "+ i + " " + n2[i]);
		}
	}
}