Tutorial Playlist

Java Tutorial for Beginners


10 Reasons That Explain Why You Need to Learn Java

Lesson - 1

What is Java: A Beginners Guide To Java

Lesson - 2

What Is JDK? Why Do We Need It?

Lesson - 3

One-Stop Solution for Java Installation in Windows

Lesson - 4

How to Get Started With Eclipse IDE?

Lesson - 5

What Are Java Strings And How to Implement Them?

Lesson - 6

Arrays In Java: Declare, Define, and Access Array

Lesson - 7

What Are Java Collections and How to Implement Them?

Lesson - 8

What Are Java Classes and Objects and How Do You Implement Them?

Lesson - 9

How to Implement the Revolutionary OOPs Concepts in Java

Lesson - 10

What is Encapsulation in Java and How to Implement It?

Lesson - 11

What is an Abstract Class in Java and How to Implement It?

Lesson - 12

What is Inheritance in Java and How to Implement It

Lesson - 13

What is Java Interface and Why it's Needed?

Lesson - 14

What is Polymorphism in Java and How to Implement It?

Lesson - 15

What is a Java Lambda Expression and How to Implement It?

Lesson - 16

Your One-Stop Solution for Multithreading in Java

Lesson - 17

The Differences Between C++ and Java That You Need To Know

Lesson - 18

Java vs. Python: Which is the Best Programming Language?

Lesson - 19

Java vs JavaScript: Know The 8 Major Differences

Lesson - 20

Top 25 Pattern Programs in Java For Printing Numbers

Lesson - 21

Java EE Tutorial: All You Need To Know About Java EE

Lesson - 22

What is Exception Handling in Java?

Lesson - 23

What Is Java JDBC? The Complete Reference

Lesson - 24

What is Java API and The Need for Java APIs?

Lesson - 25

Introduction To Java Servlets and Its Life-Cycle

Lesson - 26

10 Best Java Frameworks You Should Know in 2021

Lesson - 27

Top 10 Brilliant Java Project Ideas You Need to Know

Lesson - 28

Top 60 Java Interview Questions and Answers in 2021

Lesson - 29

Java Programming: The Complete Reference You Need

Lesson - 30

All You Need to Know to Implement JUnit Testing in Java

Lesson - 31

What Is Junit? a Look Into the Best Java Testing Framework

Lesson - 32

Ruby on rails

Lesson - 33
Arrays In Java: Declare, Define, and Access Array

One of the most popular programming languages in the world, Java is an essential part of any web and application development professional's toolkit. While there are many components and concepts to understand this powerful language, in this article, we are going to talk about arrays in Java. Arrays are a straightforward yet essential concept of Java programming. Whether you are an experienced programmer or a beginner, you will inevitably use arrays in almost all aspects of Java programming

What is an Array in Java?

An array refers to a data structure that contains homogeneous elements. This means that all the elements in the array are of the same data type. Let's take an example:


This is an array of seven elements. All the elements are integers and homogeneous. The green box below the array is called the index, which always starts from zero and goes up to n-1 elements. In this case, as there are seven elements, the index is from zero to six. There are three main features of an array: 

  1. Dynamic allocation: In arrays, the memory is created dynamically, which reduces the amount of storage required for the code. 
  2. Elements stored under a single name: All the elements are stored under one name. This name is used any time we use an array. 
  3. Occupies contiguous location: The elements in the arrays are stored at adjacent positions. This makes it easy for the user to find the locations of its elements. 

Full Stack Java Developer Course

In Partnership with HIRIST and HackerEarthEXPLORE COURSE
Full Stack Java Developer Course

Advantages of Arrays in Java

  • Java arrays enable you to access any element randomly with the help of indexes
  • It is easy to store and manipulate large data sets 

Disadvantages of Arrays in Java

  • The size of the array cannot be increased or decreased once it is declared—arrays have a fixed size
  • Java cannot store heterogeneous data. It can only store a single type of primitives

Now that we understand what Java arrays are- let us look at how arrays in Java are declared and defined.

Define an Array in Java

Arrays in Java are easy to define and declare. First, we have to define the array. The syntax for it is: 


Here, the type is int, String, double, or long. Var-name is the variable name of the array. 

Declare an Array in Java

These are the two ways that you declare an array in Java. You can assign values to elements of the array like this:


We have declared an array arr of type integer. The size of the array is 5, meaning that it can have five elements. The array is assigned with elements for each of the index positions. We'll run a for loop to print the elements in the array. A counter variable "i" is used to increment the index position after checking if the current index position is less than the length of the array.  


After executing this program, the output that you will get using this array is as follows:


Now that you know how to create and use an array in Java, let's look at the types of arrays.

Get a firm foundation in Java, the most commonly used programming language in software development with the Java Certification Training Course.

Types of Arrays

There are three types of arrays. We use these types of arrays as per the requirement of the program. These are:

1. One-dimensional Array

Also known as a linear array, the elements are stored in a single row. For example: 

int array

In this example, we have an array of five elements. They are stored in a single line or adjacent memory locations.


Look at this example in Java code. Here, the five elements are 1, 2, 3, 4, and 5. We use a for loop to print the elements of the array. The output of this is as follows: 


2. Two-dimensional Array

Two-dimensional arrays store the data in rows and columns:


In this, the array has two rows and five columns. The index starts from 0,0 in the left-upper corner to 1,4 in the right lower corner.

In this Java code, we have a two-dimensional array. We have two rows and three columns. Brackets separate the rows, and the number of elements separates the columns. For this, we use two for loops: one for rows and one for each element in the row. When we execute this program, the result will be as follows: 


3. Multi-dimensional Array

This is a combination of two or more arrays or nested arrays. We can even use more than two rows and columns using the following code: 

Here, we are using three rows and three columns, but we are only using two for loops. Regardless of how many rows and columns are entered, the number of for loops will always be two. 

Now that we know about the types of arrays, let us look at some examples below.

Addition using Java Arrays

/* Java Program to find the sum of all the elements in an array  */


The above program will add all the elements defined in my_array[] and produce the result.


Multiplication using Java Arrays

/* Java Program to find the sum of all the elements in an array  */


The above program will multiply all the elements defined in my_array[] and produce the result.


Copying using Java Arrays

You can copy one array to another by using Arrays.copyOf() method.

The above piece of code will store the elements of the array "a" in the newly created array "b"

Cloning using Java Arrays



Java supports object cloning with the help of the clone() method to create an exact copy of an object.

FREE Java Certification Training

Learn A-Z of Java like never beforeEnroll Now
FREE Java Certification Training

Binary Search Using Java Arrays 

Learning binary search algorithms is recommended for a better understanding of arrays. All searching and sorting algorithms start with arrays. A binary search is an algorithm used to find the element in an array. It divides the array into half, and the elements in the array must be in ascending order. Then, the value of the element and the middle element of the array is compared. If the value of the element is smaller than the value of the mid element, the greater half of the array is neglected, and the search is conducted in the lower half with the same process. 

For example: 


We have an array of seven elements; suppose we have to find the element 11. The algorithm is applied, and we get the element with the index number. 

The following is a Java code for binary search:


This is the code for the binary search class. 

The following is the code for the main() program.


This program gets the elements of the array from the user and then asks the user to find the element in the array. The output of this program will be: 


First, enter the elements in ascending order. Next, you have to put the element you want to search for and the result will be as follows: 



After reading this article, you should understand the basics of arrays in Java. You learned how to declare arrays and how to assign values to them. Then you saw the three types of arrays with an example of each along with Java code. You looked at how to implement a program to add elements in an array, multiply the array elements, and how to copy and clone arrays. Finally, you got an idea to implement a binary search program that made the concept of array clear. To learn more about Java Arrays, watch this video.

Want to Kickstart Your Career in Java Development?

If you're ready to take your software development career to the next level, check out Simplilearn's Full Stack Java Developer training course. This comprehensive online Blended Learning program will teach you everything you need to know about the tools and techniques used in Java programming. Once you complete the mix of learning exercises, expert advice, and real-world industry projects, you will earn a certificate to help you land your dream job or get that promotion. What are you waiting for? 

About the Author


Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.