Java int Array - int Array in Java
int Array in Java
int Array
Java int Array
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 :
< data type > < variable >[];
< data type >[] < variable >;
Java int Array Example
Following example shows Java int Array . Save the following Java int Array example program with file name JavaIntArrayExample.java .
public class JavaIntArrayExample { public static void main(String args[]) { // JAVA INT ARRAY DECLARATION int intArray[]; // MEMORY ALLOCATION FOR JAVA INT ARRAY intArray = new int[4]; // ASSIGNING ELEMENTS TO JAVA INT ARRAY intArray[0] = 5; intArray[1] = 3; intArray[2] = 4; intArray[3] = 7; // JAVA INT ARRAY OUTPUT System.out.println("Java int Array Example"); for(int i=0;i<intArray.length;i++) { System.out.println("Element at Index : "+ i + " " + intArray[i]); } } }
Related : Java boolean Array
How to assign values to java int array at the time of declaration ?
Following Java int array example you can learn how to assign values to java int array at the time of declaration.
Following example shows How to assign values to java int array at the time of declaration . Save the following assign values to java int array at the time of declaration example program with file name AssignValuesToIntArray.java .
Assign values to java int array at the time of declaration Example
public class AssignValuesToIntArray { public static void main(String args[]) { int[] intArray = {5,3,4,7}; System.out.println("Java int Array Example"); for(int i=0;i<intArray.length;i++) { System.out.println("Element at Index : "+ i + " " + intArray[i]); } } }
Related : Java byte Array
How to declare Java int array with other Java int variables ?
Following Java int array example you can learn how to declare Java int array with other Java int variables.
Following example shows How to declare Java int array with other Java int variables . Save the following declare Java int array with other Java int variables example program with file name DeclareIntArrayWithIntVariables.java .
Declare Java int array with other Java int variables Example
public class DeclareIntArrayWithIntVariables { public static void main(String args[]) { // JAVA INT ARRAY DECLARATION // intArray IS AN ARRAY n2 IS NOT AN ARRAY int intArray[], n2; intArray = new int[4]; intArray[0] = 5; intArray[1] = 3; intArray[2] = 4; intArray[3] = 7; n2 = 555; System.out.println("Java int Array Example"); System.out.println("n2 value is : "+n2); for(int i=0;i<intArray.length;i++) { System.out.println("Element at Index : "+ i + " " + intArray[i]); } } }
Related : Java char Array
Following Java int array example you can learn how to assign Java int array to other Java int array.
Following example shows How to assign Java int array to other Java int array . Save the following assign Java int array to other Java int array example program with file name AssignIntArrayToIntArray.java .
Assign Java int array to other Java int array Example
public class AssignIntArrayToIntArray { public static void main(String args[]) { int[] intArray, intArray2; intArray = new int[4]; intArray[0] = 5; intArray[1] = 3; intArray[2] = 4; intArray[3] = 7; // ASSIGNING intArray ARRAY TO intArray2 ARRAY VARIABLE intArray2 = intArray; System.out.println("Java int Array Example"); System.out.println("intArray array values"); for(int i=0;i<intArray.length;i++) { System.out.println("Element at Index : "+ i + " " + intArray[i]); } System.out.println("intArray2 array values"); for(int i=0;i<intArray2.length;i++) { System.out.println("Element at Index : "+ i + " " + intArray2[i]); } } }
Related : Java short Array