What are Java array literals? How to Use Array Literals in Java?

Java allows to use array literals like primitive and string literals. Java defines special syntax that allows us to specify array values literally in our programs. There are actually two different syntaxes for array literals. The first, and more commonly used, syntax can be used only when declaring a variable of array type. It combines the creation of the array object with the initialization of the array elements:

int[] bitWeightsForByte = {1, 2, 4, 8, 16, 32, 64, 128};

Above statement creates an array bitWeightsForByte containing eight int elements. It should be noted that we haven't used the new keyword or specified the type of the array in this array literal syntax. The type is implicit in the variable declaration of which the initializer is a part. Also, we have not specified the array length explicitly with this syntax; it is determined implicitly by counting the number of elements listed between the curly braces. There is a semicolon following the close curly brace in this array literal that terminates the variable declaration statement.

The problem with this array literal syntax is that it works only when you are declaring a variable of array type. Sometimes we want to use the array only once (such as pass it to a method), so you don't want to bother assigning it to a variable. In Java, there is an array literal syntax that supports this kind of anonymous arrays (anonymous because they don't have names). This kind of array literal looks as follows:

String response = askQuestion("Do you want to quit?", new String[] {"Yes", "No"});

With above syntax, we use the new keyword and specify the type of the array, but the length of the array is not explicitly specified.

It is important to understand that the Java Virtual Machine architecture does not support any kind of efficient array initialization. In other words, array literals are created and initialized when the program is run, not when the program is compiled. Consider the following array literal:

String response = {"Yes", "No"};

This is compiled into Java byte codes that are equivalent to:

String response = new String[2];
response[0] = "Yes";
response[0] = "No";

Thus, if you want to include a large amount of data in a Java program, it may not be a good idea to include that data literally in an array, since the Java compiler has to create lots of Java byte codes to initialize the array, and then the Java interpreter has to laboriously execute all that initialization code. In cases like this, it is better to store your data in an external file and read it into the program at runtime.

The fact that Java does all array initialization explicitly at runtime has an important corollary, however. It means that the elements of an array literal can be arbitrary expressions that are computed at runtime, rather than constant expressions that are resolved by the compiler. For example:

Point[] points = { circle1.getCenterPoint(), circle2.getCenterPoint() };

Hope you have enjoyed reading about array literals in Java. Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!



Share this page on WhatsApp

Get Free Tutorials by Email

About the Author

is the founder and main contributor for cs-fundamentals.com. He is a software professional (post graduated from BITS-Pilani) and loves writing technical articles on programming and data structures.