Array Literals in Java - GeeksforGeeks

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.
Initialization of array elements at the time of creating array object.Using anonymous arrays
Initialization of array elements at the time of creating array object.
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 classesimport java.io.*; // Main classpublic 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 + " ");    }}
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 classesimport java.io.*; // Main classpublic 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);    }}
60.0
anikakapoor
simranarora5sos
surindertarika1234
Java-Arrays
Picked
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Constructors in Java
Stream In Java
Exceptions in Java
Different ways of Reading a text file in Java
Functional Interfaces in Java
Java Programming Examples
StringBuilder Class in Java with Examples
Checked vs Unchecked Exceptions in Java
Comparator Interface in Java with Examples
Strings in Java