Anonymous Arrays – Free Java Tutorials

Arrays can be declared without variable names, such arrays are called Anonymous arrays. Such arrays are made for single time use. Once used they can not be used again in the entire life of the program. The main purpose of creation of anonymous arrays is instant use. While creating anonymous arrays we cannot specify the size, otherwise, the compiler will generate compile time error. We can use anonymous arrays by passing it to methods as an argument.
We can create an anonymous array as

new datatype[]{datavalues}

For example:

new int[]{5, 6, 7, 8}     //valid and correct 
new int[4]{5, 6, 7, 8}    //invalid 
new int[]{5, 6, 7, 8};    //invalid

Example of anonymous arrays:

class Anonymous{
public static void main( String [] args){
sum(new int[]{1, 2, 3, 4});              //calling a method and passing anonymous array as argument
}
public static void sum(int[] x){         //definition of method
int sum = 0;
for(int i : x){
sum = sum+i;
}
System.out.println("Sum is "+sum);       //printing values of anonymous array
}
}

In the above code anonymous array is passed to the method sum as an argument. We can not use this array further in code because we don’t know how to call it.
We can also create 2-dimensional anonymous arrays as follows:

new int[][]{{4,2},{3,9,6}}

Based on our requirement, we may give a name for anonymous arrays, but then it will be no longer remain anonymous arrays and will become regular arrays.
For example:

int[] ar = new int[]{12, 13, 14};

Array Element Assignment

Case-1:

For any primitive type arrays, we can assign any value of the data types which are compatible to declare types.
For example, int type array can be assigned the values of byte, short, char and int type. If we assign values of any other types then we will get compile time error.

int[] a = new int[10];
a[0] = 10;
a[1] = 'a';
byte b = 10;
a[2] = b;
short s = 20;
a[3] = s;
//All above statements are valid
a[4] = 1l;
a[5] = 0.5;
a[6] = 0.5f;
//These statements are invalid.

Case-2:

For any non-primitive type arrays or object type arrays we can assign a value of declaring type objects or any chile class objects. For example: Consider an array of Object class.

Object[] obj = new Object[10];
a[0] = new Object();
a[1] = new String("Mark");
a[2] = new Integer(10);

All of the above cases are valid. Now consider an array of String class.

String[] str = new String[10];
str[0] = new String("Mark");    //valid
str[1] = new String("Bill");    //valid
str[2] = new Integer(10);       //invalid

In the above example, the last case is invalid because Integer is not the child class of String.


Case-3:

Creation of arrays of interface types is possible. Values of such arrays can only be objects of its implemented class.
For example: Consider an array of Runnable type.

Runnable[] r = new Runnable[10];
r[0] = new Thread();               //valid
r[1] = new String("Mark");         //invalid syntax
r[2] = new Integer(10);            //invalid syntax

In the above cases, String and Integer objects cannot be assigned to the array elements because String and Integer class do not implement Runnable interface. While Thread class implements Runnable interface, we can assign its objects to its elements.


Case-4:

In the case of abstract class type arrays, the value of array elements we can provide its child class objects. For example:

Number[] n = new Number[10];
n[0] = new Integer();   //valid syntax
n[1] = new Number();    //invalid

Array Variable Assignment

Case-1:

Values of data types of lower range can be assigned to that of higher range. But in the case of arrays, it is not applicable. Take for an instance the following example:

int [] a = {1, 2, 3};
char[] c = {a, b, c};
int[] b = a;     //valid
int[] c = a;     //invalid

Following is the valid list of conversions which are
char       -> int
int        -> long
String     -> Object
String[]   -> Object


Case-2:

Whenever we are assigning 1 array to other internal elements won’t copy, just reference variables reassignment takes place.

int[] a={1,2,3,4,5};
int b={7,8};
a=b;                //valid
b=a;                //also valid

Case-3:

We cannot assign 1-dimensional array into a 2-dimensional array and vice versa. The dimensions should always match.

int[][]a=new int[3][];
a[0]=new int[4][3];      //invalid
a[0]=111;                //invalid

Case-4:

Whenever we are assigning 1 array to another array their sizes are not mandatory to match.

class First                                              //creating a class
{
      public ststic void main(String[] args)
      {
             for(int i=0;i<=args.length;i++)             
             {
                   System.out.println(args[i]);
             }
      }
}

Inputs we are giving:

java first A B C
java first B C
java first

Output:

A
B
C 
array index out of bound exception

B
C 
array index out of bound exception

array index out of bound exception

This is all about the anonymous arrays in Java.

Leave a Comment

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