Anonymous Array in Java With Example

In Java, an Array without having any name is called an anonymous array. Using anonymous array we can pass an array with user values without the referenced variable.

Syntax to create anonymous array in Java:-
new <data type>[]{<list of values with comma separator>};

Example:-
new int[] {10, 20, 30, 40};
new char[] {‘a’, ‘b’};
new int[] {}; // empty, no use
new int[][] { {40, 50}, {10, 20, 30} }; // anonymous multidimensional array

For Anonymous array creation, do not mention size in []. The number of values passing in inside {} will become the size.

An Anonymous array can also be created with a referenced variable, but it is not recommended. It is only recommended to pass an array as an argument with explicit values without the referenced variable.
int[] a = new int[] {9, 18, 27}; // we can do it, but not recommended

Sample program using anonymous array in Java

class AnonymousArrayExample{

  public static void main(String[] args) {
    // passing an Anonymous array to a method
    display(new int[] {1,2,3,4});
  }

  // method to display array elements
  static void display(int[] a){
    for(int i=0; i < a.length; i++){
      System.out.print(a[i]+"\t");
    }
  }

}

Output:-

1 2 3 4

Note that anonymous array has no name so we can’t access it’s indexed and can’t modify its values after creation. Generally, it is used to pass or return multiple elements of the same type to another method.

When we should use anonymous array in Java?

There are three ways of array creation of Java. These ways are,

1) Array creation with values. It uses {} bracket. Example:-
int[] a = {1,2,3,4};
2) Array creation without explicit values or with a default value. It uses the “new” keyword and square bracket[]. Example:-
int[] a = new int[4];
3) Anonymous array creation. It uses an anonymous array i.e. “new” keyword, [] and {}. It is the combination of the previous two ways. Example:-
new int[] {1,2,3,4};

Now, question is when we should use which declaration?

If we already know the type of values, the number of values, and also values of the array at the time of array creation then we should use the first way of creation i.e. array creation with explicit values.

When we know only types of values and number of values to stored in the array then we should use the second way of creation i.e. array creation without an explicit value. Later we can update their value from the default value.

If there is no use of array after some lines then we should use an anonymous array. We can’t access elements of the anonymous array using an index.

Example

To understand the use of anonymous array check below program, arithmeticOperation(-,-) method. We want to write a method which takes two number as parameters and return its sum, subtraction, and multiplication. Using primitive data type we can return only one value so we need to use array. In the below program, arithmeticOperation(-,-) method return addition, subtraction, and multiplication values of the given numbers.

// Incomplete program
class Test{

  public static void main(String[] args) {
    int[] a = new int[3];
    a = arithmeticOperation(10, 5);
    System.out.println("Sum = "+a[0]);
    System.out.println("Sub = "+a[1]);
    System.out.println("multiply = "+a[2]);
  }

  static int[] arithmeticOperation(int a, int b){
    int add = a+b;
    int sub = a-b;
    int multiply = a*b;
    // write the logic
    // to return add, sub, multiply
  }

}

Now, how to write the logic to return add, sub, and multiply values. We will see the all three ways and compare them.

The first way is by using array creation with values. In array creation with values, first calculate the values of add, sub, multiply and then create an int array with these values and return them.

static int[] arithmeticOperation(int a, int b){
  int add = a+b;
  int sub = a-b;
  int multiply = a*b;
  int[] arr = {add, sub, multiply};
  return arr;
}

The second way is using array creation without explicit value or with default value. Here we must create an array having size of 3. Later calculate the add, sub, and multiple, and store them into the array. After storing return the array.

static int[] arithmeticOperation(int a, int b){
  int[] arr = new int[3];
  arr[0] = a+b;
  arr[1] = a-b;
  arr[2] = a*b;
  return arr;
}

In the third way i.e using anonymous array simply calculate the add, sub, and multiply and return it.

static int[] arithmeticOperation(int a, int b){
  int add = a+b;
  int sub = a-b;
  int multiply = a*b;
  return (new int[]{add, sub, multiply});
}

In the first two ways of array creation, we created an array variable that needs memory to store array elements. Using Anonymous array we can do the same without using any extra memory. Lines of code also increased in those two ways in comparison to an anonymous array. The most important thing to note that there is no use of these array after the return. We are creating these arrays only to return the values otherwise there is no use of any array in this method. So, we should use an anonymous array.

class Test{

  public static void main(String[] args) {
    int[] a = new int[3];
    a = arithmeticOperation(10, 5);
    System.out.println("Sum = "+a[0]);
    System.out.println("Sub = "+a[1]);
    System.out.println("multiply = "+a[2]);
  }

  static int[] arithmeticOperation(int a, int b){
    int add = a+b;
    int sub = a-b;
    int multiply = a*b;
    // return values using anonymous array
    return (new int[]{add, sub, multiply});
  }

}

Output:-

Sum = 15
Sub = 5
multiply = 50

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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