Creating an array
18 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)There are two ways to create an array. Write the values in { } when you already know them, or give only a size with new and fill the boxes later.
Create it with values: { }
Your three exam marks are already known: 82, 74 and 69. List them in braces on the line where you declare the array (give it its name and type); Java counts the values and makes an array of that size.
Example 1 · Values you already know
public class Subjects { public static void main(String[] args) { String[] subjects = {"Maths", "Physics", "Chemistry"}; int[] marks = {82, 74, 69}; System.out.println(subjects[0] + ": " + marks[0]); System.out.println(subjects[2] + ": " + marks[2]); }}Maths: 82 Chemistry: 69
What this code does
- Both arrays are created and filled on the line that declares them.
- Java counts 3 values in each
{ }, so both arrays have size 3 and the indexes 0 to 2.
This short form works only on the declaration line. On a later line, marks = {82, 74, 69}; gives the compile error illegal start of expression; write marks = new int[] {82, 74, 69}; instead.
You may also see int marks[]. It works too, but int[] marks keeps the whole type, int[], together.
Create it with a size: new
A billing app knows it needs 3 prices, but not the prices yet. new creates an array of empty boxes; the number in [ ] is its size, and it may be a variable.
Example 2 · Create with a size, fill later
public class Prices { public static void main(String[] args) { int items = 3; double[] prices = new double[items]; // 3 boxes prices[0] = 49.5; prices[1] = 120.0; prices[2] = 15.75; System.out.println("Pen: Rs. " + prices[0]); System.out.println("Book: Rs. " + prices[1]); System.out.println("Eraser: Rs. " + prices[2]); }}Pen: Rs. 49.5 Book: Rs. 120.0 Eraser: Rs. 15.75
What this code does
- The size comes from the variable
items, so the array has 3 boxes. prices[0] = 49.5;puts a value into box 0, like assigning to a normal variable. Each box is filled on its own line.
Leaving the size out, as in new int[], gives the compile error array dimension missing.
Default values
You make new int[6] for the runs of an over and print a box before filling it. Java never leaves a box empty: it fills every box of a new array with a default value for its type.
Example 3 · Default values after new
public class Defaults { public static void main(String[] args) { int[] runs = new int[6]; double[] temps = new double[7]; boolean[] present = new boolean[30]; String[] names = new String[3]; System.out.println(runs[0]); System.out.println(temps[6]); System.out.println(present[10]); System.out.println(names[1]); }}0 0.0 false null
What this code does
- No values were given, so each box holds the default for its type: 0 for
int, 0.0 fordouble,falseforboolean. - A
Stringbox holdsnulluntil you put text in it.
null means "no text yet". It is not the same as empty text, "".Spot the creation mistake
Each program below should create an array, but one line does not compile. Tap the wrong line, then check the fix.
Find the bug
Find the line that does not compile
Select the line that is wrong.
This should store 3 marks.
Round 1 of 4Next step
Check your understanding
9 quick questions