• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

Java Array | Declaration, Initialization, Example Code

Irfan Khan/291/0
CodeJAVA

Array in Java is an important concept and, in this article, we are going to understand the array and its working.

Java array is an object that is used to hold multiple elements of the same data type in a single variable. The length of the array is fixed and it is established when the array is created. The elements of the array have contiguous memory allocation and can be accessed by numerical index.

Array index begins from 0 i.e., the first element will have the 0th index, the second element will have the 1st index, and so on. The length of the array is fixed and it is established when the array is created.

Table of Contents

  • How to Declaring Array?
  • Creating, Initializing, and Accessing an Array
  • Java Array Example
  • Advantages of Array
  • Disadvantages of Array

How to Declaring Array?

Declaring an array is just like declaring a variable. We need to write the data type of array and then the name of the array. Type of array is written as type[] where type is any data type of array like int, float, etc.

Here, the square bracket indicates that the variable is an array. The name of the array can be anything but must follow the naming convention. Declaration only specifies the name and type of variable so declaring an array does not mean creating an array, it simply indicates that the variable will contain an array of specified datatype.

See the below declaration:

int[] arr; 

This will create a variable named arr as an integer array.

You can also place the brackets after the name of the array. But according to the Java coding convention, it is not advised to use this form as brackets are used to identify array type so it should appear with the type designation.

Creating, Initializing, and Accessing an Array

To create an array, we need to use the new operator as we did here:

arr= new int[5]

If the above statement is compiled, an integer array arr of size 5 will be created and memory space will be allocated to it.

We can initialize an array by assigning values to the elements of the array. We can do so by accessing the array using the index value.

arr[0]=1;  // first element of arr initialised

There is also an alternate way to create and initialize the array together. It is good if you want to create and initialize the array at the same time.

int[] arr={1, 2, 3}

Here, the array arr is initialized with values 1, 2, and 3. The size of the array will be automatically calculated by counting the number of elements inside the curly braces.

Java Array Example

We can understand all of these from the below example:

public class Main
{
  public static void main(String[] args) {
  //declaring an array
  int[] arr;
  //creating an array
  arr=new int[5];
  //initializing array
  arr[0]=1;
  arr[1]=2;
  arr[2]=3;
  arr[3]=4;
  arr[4]=5;
  //another way to declare, create and initialize an array
  char[] arr2={'a', 'b', 'c'};
  //finding the length of array
  int len=arr.length;
  System.out.println("Elements of arr:");
  //accessing elements using for loop
  for(int i=0;i<len;i++)
  {
    System.out.print(arr[i]+" ");
  }
  System.out.println("\nElements of arr2:");
  //accessing elements using for loop
  for(int i=0;i<arr2.length;i++)
  {
    System.out.print(arr2[i]+" ");
  }
}
}

Output:

Elements of arr: 
1 2 3 4 5 
Elements of arr2:
a b c

In the above example, we have used the Java flow control statements (for loop) to print all elements of the array. We can also perform the same task using the for-each loop.

If you want to iterate array elements using the for-each loop then see the below example:

public class Main
{
  public static void main(String[] args) {

    char[] arr={'a', 'b', 'c'};
    //using for-each loop
    for(char i:arr)
    {
      System.out.println(i);
    }
  }
}

Output:

a
b
c

Advantages of Array

  • The array allows us to store multiple values in a single variable.
  • We can easily access the element of the array using its index value.
  • Arrays are faster as compared to the primitive data types.

Disadvantages of Array

  • They have a fixed size. Its size cannot be changed at runtime.
  • There are chances of memory wastage.

Conclusion

The array is a data structure that allows us to store multiple values in a single variable. Although array has some disadvantages but it is widely used for many purposes. So, it is always advised to learn and understand the concept of an array before applying.

Java
Irfan Khan
I’m a software programmer having more than 5 years of development experience in Java and related technology. I write technical content as well and love to share the technical knowledge to make it available for all.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Leave a Reply Cancel reply

Basic Java Tutorial

  1. Java- Tutorial Overview
  2. Java- Features & Characteristics
  3. Java-  Installation & Setup
  4. Java- Hello, World Program!
  5. Java- JDK vs JVM vs JRE
  6. Java- Data Types & Variables
  7. Java- String & its Methods
  8. Java- Different Operators Types
  9. Java- Flow Control Statements
  10. Java- Array
  11. Java- Exception Handling
  12. Java- ‘throw’ vs ‘throws’ Keyword
  13. Java- RegEx
  14. Java 12- New Advanced Features

Java OOPs concepts

  1. Java- OOPs Introduction
  2. Java- Classes & Objects
  3. Java- Constructor & Overloading
  4. Java- Method Overload vs Override
  5. Java- Access Modifiers
  6. Java- Abstraction
  7. Java- Inheritance
  8. Java- Interfaces
  9. Java- Nested Inner Classes

Java Advanced

  1. Java- Applet vs Application
  2. Java- HashMap Collections
  3. Java- ArrayList
  4. Java- LinkedList
  5. Java- HashSet
  6. Java- HashMap vs HashSet
  7. Java- Reverse Linked List

Java Exercise

50+ Java Coding Questions [Practice]

Java Projects

Patient Billing Software

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard