How to declare Arrays in Java with Examples


In Java, an array is the fundamental data structure that allows you to store multiple values of the same data type in a single variable.


Syntax: Declaring an Array

    dataType[] arrayName = new dataType[length];
    datatype : The data type of the elements to be stored in the array.
    [ ] : It indicates that a data type is used to declare an array.
    arrayName : It is the identifier, the name that you define for the array you create.
    new : To create an instance of the array.
    dataType[length] : To declare the size/length of the array as type int (max value can be Integer.MAX_VALUE)

Examples of declaring Arrays in Java

    int[] intArray = new int[5]; // an array of type int of size 5
    String[] namesArray = new String[10]; // an array of type String of size 10
    Object[] objArr = new Object[100]; // an array of type Object of size 100
    2D array visual representation
    Note that all elements of an array are initialized to the default of the primitive datatypes and null for object types.
    Datatype Default value for array elements
    byte 0
    short 0
    int 0
    float 0.0
    long 0
    double 0.0
    char '\u0000' (null character)
    boolean false
    Object null

    Let's take a look at a few examples with multi-dimensional arrays declaration,

    int[][] intArray = new int[5][10];
    String[][] namesArray = new String[5][2];
    Object[][][] objArr = new Object[3][2][2];

    In the first example we declared a 2D array named intArray that holds integer values. The new int[5][10] statement creates a 2D array with 5 rows and 10 columns, which means it can hold up to 50 integer values.

    Let's see this as a diagram how a 2D array looks like.

    How a 2D Array looks like in Java

    Similary, you we can have a 3D array, our example Object[3][2][2] can be represented as follows,

    3D Array in Java visualization
    objArr:
    [
      [
        [objArr[0][0][0], objArr[0][0][1]], 
        [objArr[0][1][0], objArr[0][1][1]]
      ], 
      [
        [objArr[1][0][0], objArr[1][0][1]], 
        [objArr[1][1][0], objArr[1][1][1]]
      ], 
      [
        [objArr[2][0][0], objArr[2][0][1]], 
        [objArr[2][1][0], objArr[2][1][1]]
      ]
    ]
    

Access modifiers and Specifiers and Arrays

    In Java, access modifiers and specifiers can be applied to array variables, just like any other variable.


    Access Modifiers:

    • public: To allow the array to be accessed from anywhere in the code.
    • private: To restrict the array to be accessed only within the same class.
    • protected: To allow the array to be accessed within the same class, subclasses, and classes in the same package.
    • default: To allow the array to be accessed within the same package only.

    Access Specifiers:

    • final: To make the array constant and its value cannot be changed once initialized.
    • static: To make the array belong to the class, rather than to an instance of the class.
    • volatile: To indicate that the value of the array may change at any time due to concurrent modifications from multiple threads.
    • transient: Indicates that the array should not be included in the serialization process.
    • synchronized: To allow only one thread at a time to access the array.

Most important points to remember when declaring Arrays in Java

    1. In Java Arrays are indexed from 0 and not 1.
    2. You can declared an Array as instance, local or static variables, or method parameters.
    3. The length of an array in Java is fixed at the time of creation and cannot be changed later.
    4. You can create a new array with a different size and copy the elements from the old array if you want to change the size of an array.
    5. All elements are initialized to a default value of the datatype as you create it.
    6. The default values of the Object type are set to null.
    7. The default value for the boolean type is false.

Extra tip:

    You can declare and initialize an array in one step as follows,

    int[] intArray = {1, 2, 3, 4, 5};
    String[] namesArray = {"Sam", "Dean", "Charlie","Bobby","Castiel"};
    

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org



















Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap