Skip to content
Related Articles

Related Articles

Improve Article

Array Literals in Java

  • Last Updated : 14 Aug, 2021

Literal is just unlikely any constant value that can be assigned to a variable.

Illustration:

int x = 10; 
// Here 10 is a literal.

Now let us stick to the array literals that are just like primitive and string literals java also allows us to use array literals. Java defines special syntax that allows us to initialize array values literally in our programs. 

Methods: 

There are two different ways of creating array literals.



  1. Initialization of array elements at the time of creating array object.
  2. Using anonymous arrays

Method 1: Initialization of array elements at the time of creating array object. It is the most commonly used syntax and can only be used when declaring a variable of array type.

Syntax:

int[] factorsOf24 = { 1, 2, 3, 4, 6, 12, 24 };

Implementation:

The above statement creates an array of int data types containing 7 elements. Here, we:

  • Created an array object without using new keyword.
  • Didn’t specify the type of the array.
  • Didn’t explicitly specify the length of the array object.

Example

Java




// Java Program to Illustrate Array Literals by
// Initialization of array elements
// at the time of creating array object
 
// Importing required classes
import java.io.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Initializing array elements at the time of
        // creating an array object
        int[] factorsOf24 = { 1, 2, 3, 4, 6, 12, 24 };
 
        // Iterating using for each loop
        for (int i : factorsOf24)
 
            // Print the elements
            System.out.print(i + " ");
    }
}
Output
1 2 3 4 6 12 24 

Note: There is a flaw in this method as this array literal syntax works only when you assign the array object to a variable. If you need to do something with an array value such as pass it to a method but are going to use the array only once. In this case, we can avoid assigning it to a variable, but this syntax won’t allow us to do so.

Method 2: We have another syntax that allows us to use anonymous arrays. Anonymous arrays are arrays that are not assigned to any variables and hence, they don’t have names).

Syntax: 

double volume = computeVolume(new int[] { 3, 4, 5 });

Implementation:

The above statement call a method computeVolume with an anonymous array. Here, we:  

  • Didn’t store the array in any variable.
  • Used new keyword for creating an array object.
  • Explicitly specified the type of array.

Example 

Java




// Java Program to Illustrate Array Literals
// Using anonymous arrays
 
// Importing required classes
import java.io.*;
 
// Main class
public class GFG {
 
    // Method 1
    // To compute the volume
    public static double computeVolume(int[] dimensions)
    {
 
        int l = dimensions[0];
        int b = dimensions[1];
        int h = dimensions[2];
 
        // returning the volume
        return (l * b * h);
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
        // Passing an array object directly to a method
        // without storing it in a variable
        double volume
            = computeVolume(new int[] { 3, 4, 5 });
 
        // Print and display the volume
        System.out.print(volume);
    }
}
Output
60.0

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :