BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Array Declaration and Initialization

Last Updated: June 3, 2024 by Chaitanya Singh | Filed Under: java

In Java, an array is used to hold fixed number of similar type elements. The length of an array is fixed, which cannot be changed after it is created (to have variable length refer ArrayList). In this guide, we will see various examples of Array declaration and initialization in Java.

Declaring an Array

You can simply declare an array by specifying the data type of elements it will hold, followed by square brackets ([]) then followed by array name. See the example below.

int[] myArray;

Alternatively, you can declare an array like this as well.

int myArray[];

Both of these array declarations are valid, but the former is frequently used.

Creating an Array

Once the array is declared, you need to create it using the new Keyword. You need to also specify the size of array, which means how many elements it can hold (also known as length of array).

// creates an array that can hold upto 10 integers
myArray = new int[10];

Declaration and creation in single line: You can do declaration and creation of array in one statement:

int[] myArray = new int[10];

Initializing an Array

Initializing means assigning the elements to the array. This can be done during declaration as well, see the following snippet:

int[] myArray = {1, 2, 3, 4, 5};

Let’s see this for other types of array:

double[] myDoubleArray = {1.1, 2.2, 3.3};
String[] myStringArray = {"Hello", "World"};
char[] myCharArray = {'a', 'b', 'c', 'd'};

Examples

1. Declaring, Creating, and Initializing an Integer Array:

int[] numbers = new int[5]; 
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;

    2. Declaring and Creating an Array, then Initializing Using a Loop:

    // Declare and create an array of 5 integers
    int[] numbers = new int[5];

    for (int i = 0; i < numbers.length; i++) {
    // Initialize each element to i * 10
    numbers[i] = i * 10;
    }

    3. Direct Initialization of an Array:

    // Declaration, creation, and initialization in one line
    int[] numbers = {10, 20, 30, 40, 50};

    Multi-Dimensional Arrays

    In the above example, we learned how to declare, create and initialize a 1D Array, let’s see how to do the same for 2D array.

    Declaring, Creating, and Initializing a Two-Dimensional Array:

    // Declare and create a 3x3 matrix
    // 3 rows and 3 columns
    int[][] matrix = new int[3][3];

    // Initialize the first row, first index represents row
    // and second index represents column
    matrix[0][0] = 1; //first row, first column
    matrix[0][1] = 2; //first row, second column
    matrix[0][2] = 3; //first row, third column

    // Initialize the second row
    matrix[1][0] = 4;
    matrix[1][1] = 5;
    matrix[1][2] = 6;

    // Initialize the third row
    matrix[2][0] = 7;
    matrix[2][1] = 8;
    matrix[2][2] = 9;

      Direct Initialization of a Two-Dimensional Array: Short and simple version of above statements:

      int[][] matrix = {
      {1, 2, 3},
      {4, 5, 6},
      {7, 8, 9}
      };

      Array of Objects

      Till now, we learned how array handles primitive types. However an array can hold objects as well. For example, an array of String objects:

      String[] names = new String[4];
      names[0] = "Chaitanya";
      names[1] = "Rahul";
      names[2] = "Aditya";
      names[2] = "Aarav";

      // One line version of above statements
      String[] names = {"Chaitanya", "Rahul", "Aditya", "Aarav"};

      Summary

      Let’s summarize everything briefly:

      • Declaration: data_type[] arrayName; For example: int[] numbers;
      • Creation: arrayName = new data_type[size]; For example: numbers = new int[5]
      • Initialization: arrayName[index] = value; or type[] arrayName = {value1, value2, ...}; For example: numbers[0] = 1; or numbers = {1, 2, 3, 4, 5};

      Top Related Articles:

      1. How to print 2D Array in Java
      2. How to Compile and Run your First Java Program
      3. Java Scanner class with examples
      4. Java Arrays Methods
      5. Difference between length of Array and size of ArrayList in Java

      About the Author

      I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

      – Chaitanya

      Leave a Reply Cancel reply

      Your email address will not be published. Required fields are marked *

      Java Tutorial

      Java Introduction

      • Java Index
      • Java Introduction
      • History of Java
      • Features of Java
      • C++ vs Java
      • JDK vs JRE vs JVM
      • JVM - Java Virtual Machine
      • First Java Program
      • Variables
      • Data Types
      • Operators

      Java Flow Control

      • Java If-else
      • Java Switch-Case
      • Java For loop
      • Java while loop
      • Java do-while loop
      • Continue statement
      • break statement

      Java Arrays

      • Java Arrays

      OOPs Concepts

      • OOPs Concepts
      • Constructor
      • Java String
      • Static keyword
      • Inheritance
      • Types of inheritance
      • Aggregation
      • Association
      • Super Keyword
      • Method overloading
      • Method overriding
      • Overloading vs Overriding
      • Polymorphism
      • Types of polymorphism
      • Static and dynamic binding
      • Abstract class and methods
      • Interface
      • Abstract class vs interface
      • Encapsulation
      • Packages
      • Access modifiers
      • Garbage Collection
      • Inner classes
      • Static import
      • Static constructor

      Java Exception Handling

      • Exception handling
      • Java try-catch
      • Java throw
      • Java throws
      • Checked and Unchecked Exceptions
      • Jav try catch finally
      • Exception Examples
      • Exception Propagation

      Collections Framework

      • Collections in Java
      • Java ArrayList
      • Java LinkedList
      • Java Vector
      • Java HashSet
      • Java LinkedHashSet
      • Java TreeSet
      • Java HashMap
      • Java TreeMap
      • Java LinkedHashMap
      • Java Queue
      • Java PriorityQueue
      • Java Deque
      • Comparable interface
      • Comparator interface
      • Collections Interview Questions

      MORE ...

      • Java Scanner Class
      • Java 8 Features
      • Java 9 Features
      • Java Conversion
      • Java Date
      • Java Multithreading
      • Java I/O
      • Java Serialization
      • Java Regex
      • Java AWT
      • Java Swing
      • Java Enum
      • Java Annotations
      • Java main method
      • Java Interview Q

      Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap