Java Strings
You want a name to hold letters that you write between one pair of quotes in the file, and you want one writing line to show those letters. On the last page the name held a whole number, and that number had no quotes around it. On this page the value is letters, and the quotes are what hold them. String plant = "leaf"; stores the four letters leaf. System.out.println(plant) writes leaf. The quotes stay in the file. They are not on the screen. You are not reading the keyboard. You are not installing Java again, and you are not learning a second way to end a line. You already know a name can hold the word leaf. The new work is the pair of quotes itself: what sits between them, and what happens when that pair is missing.
The quotes hold the letters
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 rename public class Mint. The last page kept int count = 4; with no quotes and no keyboard. This page does not keep that number. Delete the int line if it is still there. Delete any import line, any reader line, and any nextLine line. The file you keep has no import, no Scanner, and no nextLine. An unused import does not by itself make javac fail, and it also does not write the letters. The letters are written only by the writing line that uses the name.
Inside main, the storing line is String plant = "leaf";. String is the kind word. It says this name holds letters. plant is the name, and it starts with a small p. The = sign stores the letters. The pair is next. The first quote opens the letters. The four letters are l, e, a, f, in that order, all small. The second quote closes the letters. Nothing else is between those two quotes: no space, no extra letter, no second word. The semicolon ends the line. This line does not write the screen. Storing is not writing. The screen stays blank through this line. The quotes are part of the storing line. They are not part of the value the name holds after the line has stored it.
The writing line is the bare name: System.out.println(plant);. No quotes. Java reads the name plant, writes the letters that name is holding, and ends the line. For the file you keep, those letters are l, e, a, f. The quote marks are not on the screen. The letters of the name, p, l, a, n, t, are not on the screen. String is not on the screen. System is not on the screen. The semicolon is not on the screen. 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, plant. One pair of quotes. Four letters between them, leaf. One writing line, and that line uses the bare name.
public class Mint {
public static void main(String[] args) {
String plant = "leaf";
System.out.println(plant);
}
}
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. None of those words are the letters on the screen. The word String on the main line is still not a storing line. The storing line is the later one, String plant = "leaf";. That line stores the letters. It writes nothing. System.out.println(plant); is the only writing line. It writes leaf and ends the line. One line. No quote marks on that screen line.
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. A pair of quotes in the file does not make javac write a line, and it does 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 letters were already in the file. If java Mint waits, the file still has nextLine in it. Delete that line and keep String plant = "leaf";. The program finishes after the writing line.
The screen shows leaf, one line, four letters, no quote marks. If the screen shows plant, the writing line has quotes around the name: System.out.println("plant");. Those quotes mark the letters p, l, a, n, t. Java does not look up the name, so the stored leaf is not written. The file you keep has the bare name on the writing line, and the quotes around leaf on the storing line. If the screen shows mint, you are still on an earlier file. If the screen shows 4, the file still has the whole number from the last page, or the letters between the quotes are the digit 4. The file you keep has leaf, not 4. You do not reinstall Java for a screen that shows the wrong line.
A capital letter between the quotes is stored as that capital. String plant = "Leaf"; would make the writing line write Leaf, with a large L. The file you keep does not use that. It uses four small letters. Judge the screen against the letters that actually sit between the quotes, not against a word you only meant to write.
A space is one of the letters
The writing line can stay put while the letters between the quotes change. Under the first writing line, store a new set of letters in the same name, then write the name again. The new storing line is plant = "leaf stem";. It does not start with String. plant already exists, so this line only replaces the letters the name holds. Between the quotes this time there are nine characters: l, e, a, f, a space, s, t, e, m. The space is one of them. It is not a break into two storing lines, and it is not deleted when the screen is written. The next writing line is again System.out.println(plant);. The screen now has two lines. The first is leaf, from the letters the name held at that moment. The second is leaf stem, from the letters the name holds after plant = "leaf stem";. One space sits between leaf and stem on that second line. The two words are not on two separate lines.
This is the second file. It is not the file you keep. The file you keep has one storing line, the letters leaf, and no space. Save this second file as Mint.java when you want to see the space, then put the single storing line and the single writing line back.
public class Mint {
public static void main(String[] args) {
String plant = "leaf";
System.out.println(plant);
plant = "leaf stem";
System.out.println(plant);
}
}
Type javac Mint.java. The file is fine, so javac writes no line. The second storing line is allowed to omit String, and that omission does not make javac fail. Then type java Mint. The screen shows leaf, and the next line is leaf stem. Two lines. No quote marks. The name plant is on neither line. If the second line is leafstem, with no gap, the space is missing between the quotes. If both lines are leaf, the second storing line is missing or still says "leaf". If both lines are leaf stem, the first storing line already says "leaf stem", or the first writing line sits below the change. The screen follows the order of the lines in the file, and it follows the letters between the quotes, space included.
Do not write String a second time for the same name. String plant = "leaf stem";, placed under a line that already created plant, is a different mistake. That line tries to create plant again. javac writes an error and does not stay quiet. You do not need the exact words of that error. Do not type java Mint yet. Delete the second String and leave plant = "leaf stem";. The name is created once. Later lines only change the letters it holds. On the file you keep there is no second storing line. After you have seen leaf and then leaf stem, put that file back: one String plant = "leaf"; and one System.out.println(plant);.
Nothing between the quotes
A pair can be empty. String plant = ""; has the opening quote and the closing quote, and no letter between them. The kind word is still String. The name is still plant. The = sign still stores. What it stores is no letters. This is allowed. javac does not write an error for an empty pair. System.out.println(plant); still runs. It writes the letters the name holds, and there are none, so the screen line has no letters. The line still ends. The program still finishes. You can see a blank line where leaf would have been. That blank line is the writing. It is not a wait, and it is not a failure of javac.
This is the third file. It is not the file you keep. The file you keep has leaf between the quotes. Save this third file only until you have seen the blank line, then put "leaf" back between the quotes.
public class Mint {
public static void main(String[] args) {
String plant = "";
System.out.println(plant);
}
}
Type javac Mint.java. The empty pair is allowed, so javac writes no line. Then type java Mint. The program writes a line with no letters and finishes. It does not wait. If you still see leaf, the quotes still have leaf between them. If java Mint waits, nextLine is still in the file. After you have seen the blank line, make the storing line String plant = "leaf"; again.
Without the quotes, leaf is a name
The pair is doing the work. Take both quotes away and the line is String plant = leaf;. There is no pair, so leaf is not letters. Java reads leaf as a name, and this file has no name leaf. javac writes an error. It does not stay quiet. There is no screen to read, because you do not type java Mint while javac is writing an error. You do not need the exact words of the error. The useful fact is that javac wrote a line. The repair is to put the pair back, so the line is String plant = "leaf"; again.
One quote is not a pair either. String plant = "leaf; opens the letters and never closes them before the semicolon. javac writes an error for that line too. It does not store half a word, and it does not guess where the letters were meant to end. Put the closing quote back before the semicolon. The file you keep has both quotes, with leaf between them.
This fourth file is the missing pair. It is not the file you keep. Save it only until javac writes the error, then put the quotes back around leaf.
public class Mint {
public static void main(String[] args) {
String plant = leaf;
System.out.println(plant);
}
}
Type javac Mint.java. javac writes an error. Do not type java Mint. Put a quote before l and a quote after f, so the line is String plant = "leaf";. Save. Type javac Mint.java again. Now javac writes no line. Then java Mint writes leaf.
The name still has to exist before a line uses it. If System.out.println(plant); is written above String plant = "leaf";, javac writes an error. The writing line names plant before plant exists. Move the String line above the writing line. This is the same order you already used for count and for reader. The file you keep already has that order.
Letters still do not fit a whole number. int plant = "leaf"; asks an int name to hold letters. javac writes an error. The repair on this page is not to delete the quotes. The repair is to keep the kind word String, because the quotes are holding letters. The last page kept int and kept 4. This page keeps String and keeps "leaf".
You can now store letters between one pair of quotes and write those letters without writing the quotes. String plant = "leaf"; stores leaf. System.out.println(plant); writes leaf and ends the line. A later plant = "leaf stem"; changes the stored letters without a second String, and the space between the quotes is one of those letters. An empty pair stores no letters, and the writing line writes a blank line. With no quotes, leaf is a name this file does not have, and javac writes an error. The next page stays with these letters. It looks at questions you can ask about the letters a name is already holding.
The screen is the letters between the quotes
The file you keep writes leaf. The name on the writing line is plant, and plant is not on the screen. The quotes are in the file, around leaf, and they are not on the screen. One println is one line. A space between the quotes stays on that line. An empty pair writes a line with no letters.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 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