Java Array | Initialization | Length | Methods | Types with Examples

Java Array is an object that contains elements(items) of a similar data type. For example data of Integer or Strings etc. Java Array is fixed number of Homogeneous (same kind) data with indexing of every element. This is very useful data type in java.

This Java array tutorial you will learn about what is Array in Java, some useful methods, how to Initialization and types with relevant examples.

Java Array overview and Initialization Length Methods Types with Examples

About Arrays:

  1. Fixed in size.
  2. Homogenous elements
  3. The underlying Data structure (DS)

Syntax & Initialization: 

Here is Java Array Initialization syntax.

Java Array example

Here is an example of creating and Initialization array values. This example we are creating an integer array with a size of 7.

Here How it will be indexed and store values.

Java Array Initialization Length Methods Types with Examples

Print Array 

You learned how to create a Java Array, now how to you will print the complete value or specific indexed values? for that See below code. We are using for loop to traversing an Array and print data. And for Single value fetch you just need to pass the index value. Run the loop for array we required the size of the array, that can be done by using length method.

Output: 7 3 9 3 4 8 6
Value of index 2 is 9

Java Array Methods

We are looking at only one method, more methods will be explained in another tutorial.

Get Array Length:

Get the Java array length or size you can use in build methods length

Output: 3.

Declare String Array in Java

Here is an example and how to create a string array in java.

For print the Array value you can follow the same steps of Int Array in Java example.

The advantage of Array:

  1. In the array, we can represent multiple values with a single variable.
  2. Reusability of the code will be improved.

Java Array types:

There are two types of Array in Java.

  • Single Dimensional Array: Upper examples are all Single Dimensional Array.
  • Multidimensional Array: data is stored in row and column-based index (also known as matrix form).

Limitations of object type arrays:

  1. Arrays are fixed in size: Once we created an array with some size there is no chance to increase and decrease its size based on our requirement, Hence to use arrays compulsory we should know the size in advance which may or may not possible.
  2. Homogeneous: Arrays can hold only homogeneous data elements.


Student[] s = new Student; (Correct)

S[0]             = new Customer; (Wrong)

Compiler Exception: Incompatible type

Found: Customer

Required: Student

But we can resolve this problem by using object arrays.

Object[] o = new Object[1000];

o[0]           = new Student[];

o[1]           = new Customer[];

Arrays Concept is not implemented based on some standard data structure hence readymade method support is not available for every requirement we have to write the code explicitly which is the complexity of programming.

Suppose we want to insert an element in some sorting order than who will respond to write the sorting code programmer.

Second, If we want to search whether the particular element is present or not. So in arrays, we will have to write the searching codes.

Array Error : ArrayIndexOutOfBoundsException

If you are inserting or accessing the value of from array and exceeding(greater value) the length or negative then Array Error will throw.

here is an example, our Array size is 3 or length is 4 then trying to get the value in index 5.

Output Error Screenshot:

Java Array Error ArrayIndexOutOfBoundsException in java array tutorial

Sidenotes: It’s also called One Dimensional Array or 1D Array in java.

Do comment if you have any doubt in this tutorial, and you can give suggestion, examples.

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1

Java version 11

All Java Array Examples are in Java 11, so it may change its different from Java 9 or 10 or upgraded versions.

One thought on “Java Array | Initialization | Length | Methods | Types with Examples

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.