List the values in braces at the declaration: int[] nums = {1, 2, 3};. The array gets its size from the values. Anywhere else (a method argument, a return, a reassignment) write new int[] {1, 2, 3}, and when you know the size but not the values yet, use new int[5], which starts every element at 0.
A Java array has a fixed length and one element type, and there are three ways to create one: the array initializer {...}, which only works in a declaration; the array creation expression new int[] {...}, which works anywhere; and new int[n], which makes an array of a given size filled with default values. After that come the helpers that fill an array for you (Arrays.fill, Arrays.setAll and IntStream) and arrays of arrays for grids. The examples print with Arrays.toString, because printing an array directly doesn't show its elements. Each one runs on this page: hit Run, then edit the code and run it again.
1The array initializer {...}Recommended
When you know the values up front, write them between braces right in the declaration. Java counts them to set the length, so there is no size to keep in sync. It works for any element type: primitives such as int and double, String, or your own classes. Elements are read and written by index, starting at 0.
Output
Prints [2, 3, 5, 7, 11], [Java, Go, Python], [9.99, 24.5, 3.0], the length 5, then Java, [Java, Go, Kotlin] and 0. A trailing comma is allowed, so {1, 2, 3,} is a three-element array, which keeps diffs clean when you list one value per line. The brackets can also go after the name, C-style (int primes[] = {...}), but int[] primes is the standard Java style because it keeps the whole type in one place. Printing primes itself gives a type code and hash starting with [I@, not the numbers; see print an array for the ways to show the contents.
2new int[] {...}: an array in-line, anywhere
The bare {...} form is only allowed where a variable is declared. Everywhere else, put new and the array type in front of the braces: new int[] {4, 8, 15}. That expression creates an array without naming it, so you can pass it straight to a method, return it, or assign it to a variable that already exists. Leave the brackets empty: the size still comes from the values.
Output
Prints 27, [Sat, Sun], [90, 85, 100], 2, a-b-c and x-y. Writing scores = {90, 85, 100}; instead fails to compile with illegal start of expression, and var nums = {1, 2}; fails with cannot infer type for local variable nums. You also can't give a size and values together: new int[3] {1, 2, 3} is rejected with array creation with both dimension expression and initialization is illegal. If the method takes varargs, like String.join, you can skip the array and pass the values; Java packs them into an array for you.
3new int[n]: a fixed size with default values
When you know how many elements you need but will fill them in later, create the array with a size: new int[n]. The size can be any int expression worked out at run time. Java never hands you uninitialized memory: every element starts at its type's default value, which is 0 for numbers, false for boolean, the character with code 0 for char, and null for objects such as String.
Output
The defaults print as [0, 0, 0, 0], [0.0, 0.0, 0.0], [false, false, false], 0 (the char, cast to its code) and [null, null, null]. After two assignments the names are [Ada, Linus, null]. Both lengths print 3: names.length is a field with no parentheses, while names[0].length() calls the String method (and a collection such as ArrayList uses size()). Reading names[3] throws ArrayIndexOutOfBoundsException with the message Index 3 out of bounds for length 3, because the last valid index is length - 1. An array's length is fixed once it is created; if you need to add and remove elements, use a list instead (see initialize a list in one line).
4Fill it: Arrays.fill, Arrays.setAll and IntStream
For anything other than the default value, you don't need a hand-written loop. Arrays.fill(a, value) puts one value in every slot, or in a range of slots. Arrays.setAll(a, i -> ...) (Java 8+) computes each element from its index. And for number sequences, IntStream builds the int[] directly with toArray().
Output
Prints [1, 1, 1, 1, 1], [-, x, x, -], [0, 1, 4, 9, 16, 25], [item1, item2, item3], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] and finally [0, 1, 4, 9, 16, 25, 0, 0]. IntStream.range(0, n) excludes the end and rangeClosed includes it; the three-argument IntStream.iterate with a stop condition needs Java 9+. Arrays.copyOf is the standard way to "resize": it returns a new array and pads the extra slots with the default value. To turn a List into an array, or the other way round, see convert an array to a list.
5Which should you use?
| Form | Length comes from | Starting values | Best for |
|---|---|---|---|
| int[] a = {1, 2, 3} | The values | The listed values | Known values, at the declaration |
| new int[] {1, 2, 3} | The values | The listed values | Arguments, returns, reassigning, var |
| new int[n] | n, at run time | Defaults: 0, false, null | Size known now, values later |
| Arrays.fill(a, v) | An existing array | One repeated value | A non-default starting value |
| Arrays.setAll(a, i -> ...) | An existing array | Computed from the index | Sequences, labels, lookup tables |
| IntStream.rangeClosed(1, n).toArray() | The range | Consecutive numbers | Number ranges as an int[] |
6Common variation: 2-D and jagged arrays
Java has no true multi-dimensional arrays: an int[][] is an array whose elements are int[] rows. new int[3][4] creates 3 rows of 4 zeros, and a nested initializer lists the rows one brace group at a time. Because each row is its own array, rows can have different lengths. Give only the first size, new int[4][], and create each row yourself to build a "jagged" array. Print them with Arrays.deepToString, since Arrays.toString would only show the rows' type codes.
Output
The grid prints as [[0, 0, 0, 0], [0, 0, 7, 0], [0, 0, 0, 0]] and 3 rows, 4 columns: grid.length counts rows and grid[0].length counts the columns of the first row. matrix[1][0] is 4. The triangle prints [[1], [2, 2], [3, 3, 3], [4, 4, 4, 4]], and the ragged String rows print 1 [a], 2 [b, c] and 0 []. The last line, [[9, 0], [9, 0]], is the trap: Arrays.fill stored the same int[] object in both slots, so one write shows up in every row. Create a new row per slot, with new int[2][2] or a loop, when the rows must be independent.
Frequently asked questions
How do I declare and initialize an array in one line in Java?
Put the values in braces at the declaration: int[] nums = {1, 2, 3}; or String[] names = {"Ada", "Linus"};. Java sets the length from the values, so nums.length is 3. Anywhere other than a declaration, such as a method argument or a reassignment, write new int[] {1, 2, 3} instead.
Why does scores = {90, 85}; not compile?
The bare {...} initializer is only allowed in a declaration, so assigning it to an existing variable fails with illegal start of expression. Write scores = new int[] {90, 85};. The same rule applies to var: var nums = {1, 2}; fails with cannot infer type for local variable nums, while var nums = new int[] {1, 2}; works.
What values does a new Java array start with?
Every element gets its type's default: 0 for int, long, short and byte, 0.0 for double and float, false for boolean, the character with code 0 for char, and null for objects. So new String[3] holds [null, null, null], and calling a method such as length() on an element throws a NullPointerException until you assign it.
Can I change the size of an array after creating it?
No. An array's length is fixed when it is created. Arrays.copyOf(a, newLength) returns a new, longer (or shorter) copy, padding any extra slots with the default value. If you need to add and remove elements as you go, use an ArrayList instead. A negative size is rejected at run time with java.lang.NegativeArraySizeException: -1.
What is the difference between length, length() and size()?
Arrays have a length field, read without parentheses: nums.length. Strings have a length() method: "Ada".length(). Collections such as ArrayList and HashSet have a size() method. Using the wrong one is a compile error, not a runtime surprise.
Is int a[] the same as int[] a in Java?
For a single variable, yes: both declare an int[], and int[] a is the preferred style because the whole type is in one place. They differ when you declare several variables at once: int[] c, d; makes both arrays, but int a[], b; makes a an array and b a plain int.