Array in Java

Adil Wadhwania
4 min readMar 19, 2023

--

In this article we will learn everything about one-dimensional Array in java

Arrays in java(one-dimensional)

What you can expect from this article

  1. What is an Array
  2. Declaring an array in java
  3. Assigning values in array
  4. Accessing elements of array
  5. Changing the value in array
  6. Internal working of array

Array

  • Array is a reference datatype in java, that means the reference of the array is stored in the variable rather than the data of array.
  • An array is a collection of elements of similar data type, it can be of any type like integer array, long array, float array, etc.
  • The advantage of array is that in the primitive data types we can only store one value in a variable, to store multiple value of similar type we can use arrays. It is a fundamental data structure in computer programming.

Declaration

  • Let us first declare an array and understand how it works. In this whole article we will use integer array, all the other types would be similar.
// In java we can declare in two ways

// 1st approach
int [] arr=new int[5];

//2nd approach
int arr[]=new int[5];
  • Here we have two approach to declare an array in java and both are similar, I have explained this because you will encounter either of these while studying or practicing example.
  • Now both the approach shows the syntax, how we declare arrays in java, arrays are reference data type and “new” means we are creating an object of array.
  • The value “5” written in the right side’s square bracket is the length of the array, it is compulsory to specify the size(no of elements) while declaring an array. Here in this example both the approaches will create an integer array of length “5”.

Assigning values in the Array

  • We can assign the values in the array based on the index of array, the starting index of array is always “0”.
//Adding the value in array
arr[0]=12; //first element
arr[1]=15; //second element
arr[2]=18; //third element
arr[3]=19; //third element
arr[4]=23; //third element
  • We have added the value 12, 15 , 18, 19, 23 on the index 0th, 1st, 2nd, 3rd and 4th index of array.
  • Adding values in the array is simple, right?

Accessing elements of Array

  • We can access the elements of the array in the same way as we use to assign the value, and that is using the index of array.
  • So here we will access all the elements of array using a for loop in java
// Iterating through the elements of the array
for (int i=0;i< arr.length;i++)
{
System.out.println(arr[i]); //printing the elements of array
}
  • We have use the for loop and the condition is that it will iterate till “i” is less than the arr.length, the arr.length gives us the length of array that is “5” in our case. So the loop will start from 0 and go till 4.
  • There is a simple print statement inside the loop that prints the element of array based on the index and the value of index is changed according the value of “i” in the loop.

Changing the value in array

  • Let us now update one of the element in the array, so to update a value in the array, index of array is used.
// updating the value of second index in array
arr[2]=21;
System.out.println(arr[2]);//printing the updating value
  • Updated the value at index no 2 in the array, the value is changed from 18 previously to 21 now.
  • After that just printed the updated value using the print statement to verify whether the value is updated or not.
  • This is all about, how one-dimensional array works in java.

Internal working of array

Internal working of array in java
  • Now let us understand what happens internally when you declare an array, I have illustrated that in the above image.
  • First the continuous block of memory based on the length(in our case length is 5) is assigned in the heap memory(In java objects are stored in heap memory) at runtime.
  • All the elements in the array are assigned by a default value, and the default value is decided by the type of array, here we have a integer array so the default value of each element in array is “0” as shown in image.
  • The base address(“54” in the image is an assumption of “arr” base address) of array is then stored in the reference variable “arr” on the stack as show in the image.
  • To access an element in array using its index, the formula to find the address of that element is given below.

Address of arr[i]=base address of arr+w*(I-LB)

  • w= the number of bytes occupied by each element in array(integer take 4 bytes of space).
  • I=The index that we want to access.
  • LB = It is the lower bound of array(0 because array’s index starts with 0 only.)

“Thank you for taking the time to read my article! I hope you found the information and insights shared here helpful and interesting. If you enjoyed this article, be sure to follow my article for more similar content. I’m always exploring new topics and sharing my thoughts and insights on a variety of subjects. I appreciate your support and look forward to connecting with you through my writing. Until next time!”

ADIL WADHWANIA :)

Topics :- Array, arrays, one-dimensional array, array in java, arrays in java, declare an array in java, access an element in array using java, updating the value of array using java, assign value in array using java, java, java tutorials, java for beginners, programming, programming concepts, computer science

--

--

Adil Wadhwania

Tech-savvy writer sharing insights on programming,current affairs,sports,great personalities, and new technologies. Follow for engaging and informative content.