Loading...

Java Arrays

String[] plants = { "leaf", "mint" }; stores two words under one name, and System.out.println(plants[0]) writes leaf. The screen is one line. mint is not on that line, and there is no second line.

One name holds two words

Open Mint.java. The file name stays Mint.java, because public class Mint is still the name of the file. This page does not rename the file, and it does not change public class Mint. Delete String plant = null if that line is still there. Delete plant.length and delete charAt. Delete int count if that line is still there, including (int), 40.0, 40.9, and 40. Delete double weight if it is still there. Delete boolean ripe if it is still there, including true and false. Delete any import line, any reader line, and any nextLine line. The file you keep has no import, no Scanner, no nextLine, no count, no weight, no ripe, and no lone plant that holds null. An unused import does not by itself make javac fail, and it does not write leaf either. The word is written only by the writing line that picks a slot.

Inside main, the storing line is String[] plants = { "leaf", "mint" };. String[] is the kind word. The brackets are part of that kind. They say this name can hold more than one word. plants is the name, and it starts with a small p. The = sign stores. The braces hold the words. Each word sits in its own quotes. A comma sits between the words. leaf is the first word. mint is the second word. The semicolon ends the line. This line stores two words. It does not write the screen. Storing is not writing. The screen stays blank through this line. The two words are not the number 0, and they are not the word null. Nothing is waiting off the screen under a second name. Both words are under plants.

The words you write inside the braces are the slots. Two quoted words make two slots. You do not invent a third word in the file you keep. String[] plants = { "leaf", "mint", "stem" }; stores three words. That file is allowed, and it is not the file you keep. String[] plants = { leaf, mint }; treats leaf and mint as names. This file does not have those names. javac writes an error. String[] plants = { "leaf" "mint" }; has no comma. javac writes an error. The comma is what separates the two words. String[] plants = { 40, 9 }; asks this kind to hold whole numbers. The words in the file you keep are leaf and mint, not digits. javac writes an error. The kind word with the brackets, on this page, holds words in quotes.

The writing line picks one slot: System.out.println(plants[0]);. The brackets after the name hold a number. That number is 0. Slot 0 is the first word, because the count of slots starts at 0, not at 1. There is no quote around 0. There is no dot, and there is no length. Java reads plants, reads the number 0, writes the word in that slot, and ends the line. For the file you keep, that word is leaf. The letters of the name, p, l, a, n, t, s, are not on the screen. String is not on the screen. The braces are not on the screen. The comma is not on the screen. mint is not on the screen. The word leaf is on the screen, as one line. Save the file.

The file you keep

Save this as Mint.java. It is the file this page keeps. One kind word, String[]. One name, plants. Two words in braces, leaf then mint, each in quotes, with one comma between them. One writing line, and that line picks slot 0. There is no length, no charAt, and no second writing line.

the file you keep

java
public class Mint {
    public static void main(String[] args) {
        String[] plants = { "leaf", "mint" };
        System.out.println(plants[0]);
    }
}

Read it from the top. public class Mint names the file. It does not write the screen. public static void main(String[] args) is where Java starts. The word String on that main line is still not a storing line. The brackets on that main line belong to args, and this page does not read args. The storing line is the later one, String[] plants = { "leaf", "mint" };. It writes nothing. System.out.println(plants[0]); writes leaf and ends the line. One line on the screen. No second line. There is no int line in the file you keep, and there is no line that stores null.

What the screen shows

In the folder that contains this Mint.java, type javac Mint.java. When the file is fine, that command writes no line. The brackets and the braces do not make javac write a line, and they do not make javac fail. Then type java Mint. Do not type .java after java. The same two commands are what you type on Windows and on macOS. This page does not quote the shell prompt. The program writes leaf. It does not wait for you to type. The words were stored in the file before the run. If java Mint waits, nextLine is still in the file. Delete that line and keep String[] plants = { "leaf", "mint" }; and the one writing line. The program finishes after the writing line.

The screen shows leaf. One line. If the screen shows mint, the writing line picked slot 1, or the first word in the braces is mint. Look at the file. The file you keep has leaf first and plants[0] on the writing line. If the screen shows plant, the writing line has quotes around a different name. If the screen shows the letters plants[0], the writing line has quotes around those letters: System.out.println("plants[0]");. Those quotes mark letters, so the stored word is not written. If the screen shows null, the null file is still in place, or the slots were created with no words in the braces. The file you keep has leaf and mint inside the braces. If the screen shows 40, or 40.0, or 4, the casting file, the numbers file, or a length question is still in place. Delete that line. If the screen shows true or false, ripe is still in the file. If the screen shows a blank line and not the word leaf, a storing line still has quotes with nothing between them. That is not the file you keep. You do not reinstall Java for a screen that shows the wrong line.

A screen that shows leaf does not, by itself, prove the brackets. String plant = "leaf"; also writes leaf. The screen can look the same. The file does not. The file you keep has String[], it has braces, it has a comma, and the writing line has plants[0]. When you judge this page, look for the brackets on the kind word and look for the number 0. Do not judge the file from the screen alone.

The other word sits at 1

The second word is in the file, and the kept writing line does not write it. To see mint, the number in the brackets is 1. Slot 1 is the second word, because slot 0 was the first. This run is allowed, and it is not the file you keep.

the second word

java
public class Mint {
    public static void main(String[] args) {
        String[] plants = { "leaf", "mint" };
        System.out.println(plants[1]);
    }
}

Type javac Mint.java. javac writes no line. Then type java Mint. The screen shows mint. One line. leaf is not on that line. The storing line did not change. Only the number in the brackets changed. 1 does not mean "the first word." The first word is still leaf, in slot 0. After you have seen mint, put plants[0] back. The file you keep writes leaf.

📊 The number in the brackets and the screen

The number The screen
0 leaf
1 mint
2 java Mint writes an error
String plants = { "leaf", "mint" }; javac writes an error

Read the table against a run, not instead of one. For 0, javac writes no line and java Mint writes leaf. For 1, javac writes no line and java Mint writes mint. For 2, javac writes no line and java Mint writes an error, because that slot was not in the braces. For String plants = { "leaf", "mint" }; with no brackets on the kind word, javac writes an error, so you do not type java Mint. After each check, put the kept file back: String[], braces, leaf then mint, and plants[0]. The kept screen is leaf, one line.

The name has to exist before a line uses it. If System.out.println(plants[0]); is written above the storing line, javac writes an error. The writing line names plants before plants exists. Move the storing line above the writing line. Do not write String[] a second time for the same name. A later line can replace one word without writing String[] again, once plants already exists: plants[0] = "stem";. That line only replaces the word in slot 0. Slot 1 still holds mint. It is allowed. It is not the file you keep. The file you keep has one storing line, and that line says leaf then mint.

String[] plants; with no braces and no words, followed by the writing line, makes javac write an error. The name would exist as a kind, and it would hold no words yet. Put the braces on the creating line, the way the file you keep has them. String[] plants = { "leaf" }; stores one word. plants[0] writes leaf. plants[1] is a slot you did not write, so java Mint writes an error after javac stays quiet. That one-word file is not the file you keep. A missing space does not glue the kind word into a new kind that works: String[]plants still stores the two words, and javac writes no line. The file you keep has a space after String[]. String [] plants with a space before plants still stores the two words. plants[ 0 ] with spaces inside the brackets still writes leaf. The file you keep has no extra space, but that space is not a second value.

The braces are not parentheses. String[] plants = ( "leaf", "mint" ); makes javac write an error. The words sit inside braces, { and }, not inside ( and ). The writing line is not a call with parentheses around the number. System.out.println(plants(0)); makes javac write an error. The number sits inside brackets, [ and ]. A small s on the kind word is not the kind. string[] plants = { "leaf", "mint" }; treats string as a name. This file does not have that name. javac writes an error. The kind word is String, with a capital S, then the brackets. The name stays plants, with a small p. String[] Plants with a capital P is a different name. It can store the words, and it is not the name in the file you keep. Put the small p back.

A comma after the last word does not add a slot. String[] plants = { "leaf", "mint", }; still stores two words. javac writes no line. plants[0] still writes leaf, and plants[1] still writes mint. There is no third word. The file you keep has no comma after mint, but that extra comma is not a third value. new is not required. String[] plants = new String[] { "leaf", "mint" }; also stores the two words, and plants[0] still writes leaf. The file you keep does not write new. String[] plants = new String[2]; creates two slots and puts no words in them. plants[0] then writes null. Those slots hold no letters. That is not the file you keep. The words leaf and mint have to sit inside the braces.

A slot you did not write

Two words make two slots, numbered 0 and 1. The number 2 is a third slot. You did not write a third word. The third file asks for that slot.

a slot you did not write

java
public class Mint {
    public static void main(String[] args) {
        String[] plants = { "leaf", "mint" };
        System.out.println(plants[2]);
    }
}

This is the third file. It is not the file you keep. Type javac Mint.java. The file is fine as far as javac can see, so javac writes no line. The number 2 does not make javac fail. Then type java Mint. The program does not write leaf, and it does not write mint, and it does not write 2, and it does not write null. java Mint writes an error. You do not need the exact words of that error. The braces held two words, so there is no third word to write. plants[-1] is not a way to pick the last word. javac writes no line, and java Mint writes an error. The numbers that match the file you keep are 0 and 1 only. If the screen shows mint, the number in the brackets is 1, and the second word is still there. After you have seen the error, put the kept file back.

A name that holds no slots is a different failure, and the screen is not the word null. String[] plants = null; stores no slots. javac writes no line. System.out.println(plants[0]); then makes java Mint write an error. It does not write null, and it does not write leaf. The brackets on the writing line ask for a slot, and this name has no slots. That file is not the file you keep. The file you keep has braces, and the braces hold leaf and mint.

You can now store two words under one name and write the first word. String[] plants = { "leaf", "mint" }; stores leaf and mint. System.out.println(plants[0]); writes leaf and ends the line. plants[1] writes mint, and that is not the file you keep. plants[2] lets javac stay quiet and makes java Mint write an error. A String name with no brackets cannot hold the braces. The next page leaves these brackets. It looks at a mark written between two whole numbers.

The screen is the first word

The file you keep writes leaf. The name on the writing line is plants, and plants is not on the screen. The number in the brackets is 0. mint is the second word, and it stays in the file while the screen shows leaf. A slot number you did not write is not the file you keep. Quotes around plants[0] write letters, not the word in the slot.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
5
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top