Arrays in Java - GeeksforGeeks
Arrays in Java - GeeksforGeeks
Arrays in Java - GeeksforGeeks
Arrays in Java
Difficulty Level :
Easy ● Last Updated :
24 Jan, 2021
Arrays in Java work differently than they do in C/C++. Following are some impor tant
Since arrays are objects in Java, we can find their length using the object proper ty
length. This is different from C/C++ where we find length using sizeof.
A Java array variable can also be declared like other variables with [] af ter the data
type.
The variables in the array are ordered and each have an index beginning from 0.
Java array can be also be used as a static field, a local variable or a method
parameter.
The size of an array must be specified by an int or shor t value and not long.
Ever y array type implements the inter faces Cloneable and java.io.Serializable.
Array can contain primitives (int, char, etc.) as well as object (or non-primitive)
references of a class depending on the definition of the array. In case of primitive data
types, the actual values are stored in contiguous memor y locations. In case of objects
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that
you have read and understood our
Cookie Policy &
Privacy Policy
Got It !
Creating, Initializing, and Accessing an Array
One-Dimensional Arrays :
type var-name[];
OR
type[] var-name;
An array declaration has two components: the type and the name. type declares the
element type of the array. The element type determines the data type of each element
that comprises the array. Like an array of integers, we can also create an array of other
primitive data types like char, float, double, etc. or user-defined data types (objects of
a class). Thus, the element type for the array determines what type of data the array
will hold.
Example :
int intArray[];
or int[] intArray;
byte byteArray[];
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that
short shortsArray[];
you have read and understood our
Cookie Policy &
Privacy Policy
boolean booleanArray[];
Got It !
long longArray[];
float floatArray[];
double doubleArray[];
char charArray[];
// user)
MyClass myClassArray[];
// of unknown type
Although the first declaration above establishes the fact that intArray is an array
variable, no actual array exists. It merely tells the compiler that this variable
(intArray) will hold an array of the integer type. To link intArray with an actual,
physical array of integers, you must allocate one using new and assign it to intArray.
give memor y to array, you create an array like this:The general form of new as it
Here, type specifies the type of data being allocated, size specifies the number of
elements in the array, and var-name is the name of array variable that is linked to the
array. That is, to use new to allocate an array, you must specif y the type and number
of elements to allocate.
Example :
OR
Got It !
1. The elements in the array allocated by new will automatically be initialized to zero
(for numeric types), false (for boolean), or null (for reference types).Refer Default
2. Obtaining an array is a two-step process. First, you must declare a variable of the
desired array type. Second, you must allocate the memor y that will hold the array,
using new, and assign it to the array variable. Thus, in Java all arrays are
dynamically allocated.
Array Literal
In a situation, where the size of the array and variables of array are already known,
The length of this array determines the length of the created array.
There is no need to write the new int[] par t in the latest versions of Java
Each element in the array is accessed via its index. The index begins with 0 and ends at
(total array size)-1. All the elements of array can be accessed using Java for Loop.
Implementation:
Output:
Element at index 0 : 10
Element at index 1 : 20
Element at index 2 : 30
Element at index 3 : 40
Element at index 4 : 50
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that
you have read and understood our
Cookie Policy &
Privacy Policy
Got It !
Arrays of Objects
An array of objects is created just like an array of primitive type data items in the
following way.
The studentArray contains seven memor y spaces each of size of student class in which
the address of seven Student objects can be stored.The Student objects have to be
instantiated using the constructor of the Student class and their references should be
Output:
Element at 0 : 1 aman
Element at 1 : 2 vaibhav
Element at 2 : 3 shikar
Element at 3 : 4 dharmesh
Element at 4 : 5 mohit
accessed with an illegal index. The index is either negative or greater than or equal to
size of array.
class GFG
{
public static void main (String[] args)
{
int[] arr = new int[2];
arr[0] = 10;
arr[1] = 20;
for (int i = 0; i <= arr.length; i++)
System.out.println(arr[i]);
}
}
Runtime error
at GFG.main(File.java:12)
Output:
10
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that
20
Got It !
Multidimensional Arrays
Multidimensional arrays are arrays of arrays with each element of the array holding
the reference of other array. These are also known as Jagged Arrays. A
multidimensional array is created by appending one set of square brackets ([]) per
dimension. Examples:
class multiDimensional
{
public static void main(String args[])
{
// declaring and initializing 2D array
int arr[][] = { {2,7,9},{3,6,1},{7,4,2} };
// printing 2D array
for (int i=0; i< 3 ; i++)
{
for (int j=0; j < 3 ; j++)
System.out.print(arr[i][j] + " ");
System.out.println();
}
}
}
Output:
2 7 9
3 6 1
7 4 2
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that
you have read and understood our
Cookie Policy &
Privacy Policy
Got It !
Passing Arrays to Methods
Like variables, we can also pass arrays to methods.For example, below program pass
Output :
A s usual, a method can also return an array. For example, below program returns an
Output:
1 2 3
Ever y array has an associated Class object, shared with all other arrays with the same
component type.
Got It !
Output:
class [I
class java.lang.Object
class [B
class [S
class [Ljava.lang.String;
Explanation :
1. The string “[I” is the run-time type signature for the class object “array with
3. The string “[B” is the run-time type signature for the class object “array with
4. The string “[S” is the run-time type signature for the class object “array with
5. The string “[L” is the run-time type signature for the class object “array with
Array Members
Now as you know that arrays are object of a class and direct superclass of arrays is
The public final field length, which contains the number of components of the array.
All the members inherited from class Object; the only method of Object that is not
The public method clone(), which overrides clone method in class Object and
Cloning of arrays
When you clone a single dimensional array, such as Object[], a “deep copy ” is
per formed with the new array containing copies of the original array ’s elements as
opposed to references.
Output:
false
1 2 3
which is to say that it creates only a single new array with each element array a
Output:
false
true
true
Related Ar ticle :
GeeksforGeeks and would like to contribute, you can also write an ar ticle using
See your ar ticle appearing on the GeeksforGeeks main page and help other Geeks.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that
Please write comments if you find anything incorrect, or you want to share more
you have read and understood our
Cookie Policy &
Privacy Policy
information about the topic discussed above.
Got It !