Loading...

Java Variables

You want one writing line to show whatever word a name is holding. On the last page the word sat between quotes on the writing line itself, so changing the screen meant editing that line. On this page you give the word a name, plant, and the writing line uses that name. The quotes move to the line that stores the word. System.out.println(plant) has no quotes, and the screen still shows leaf. You are not installing Java again. You are not adding a note, and you are not learning a second way to end a line. You already know println writes the letters and ends the line. The new work is the name that holds those letters.

A name holds 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 the class. Inside main, above the writing line, add a line that creates a name and stores a word in it: String plant = "leaf";. String says this name holds letters. plant is the name, and it starts with a small p. The = sign stores the word. The quotes hold leaf, all small letters, the same word the last page put on the writing line. The semicolon ends this line. Then change the writing line so it no longer has quotes. It becomes System.out.println(plant);. The name plant is bare. There is no quote before the p and no quote after the t. Save the file.

The file you keep

Save this as Mint.java. It is the file this page keeps. The name plant holds leaf. The writing line uses that name and does not contain quotes.

the file you keep

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

Read it from the top and decide what the screen can receive. 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 letters for the screen. The word String on that main line is not the String on the next line. The main line is the start. The next line is the one that creates the name. String plant = "leaf"; stores the letters l, e, a, f in the name plant. Those letters are not written to the screen by this line. Storing is not writing. The screen stays blank through this line. The following line, System.out.println(plant);, is the writing line. Java reads the name plant, takes the letters that name is holding, writes them, and ends the line. The screen shows leaf. One line. No quotes. The letters of the name, plant, are not on the screen. System is not on the screen. The semicolon is not on the screen.

Run it the same way as before. In the folder that contains this Mint.java, type javac Mint.java. When the file is fine, that command writes no line. A name 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 screen shows leaf. If the screen shows plant, the writing line has quotes around plant. The file you keep does not. If the screen still shows mint, the stored word is still mint, or the writing line is still System.out.println("mint");. Look at the letters between the quotes on the String line, and look at whether the writing line uses the bare name. You do not reinstall Java for a screen that shows the wrong word.

Change the word, keep the writing line

The point of the name is that the writing line can stay put while the stored word changes. Under the first writing line, store a new word in the same name, then write the name again. The new storing line is plant = "stem";. It does not start with String. plant already exists, so this line only replaces the letters the name holds. leaf is replaced by stem, all small letters. The next writing line is again System.out.println(plant);. It is the same shape as the first writing line. The screen now has two lines, because each println ends its line. The first line is leaf, from the letters the name held at that moment. The second line is stem, from the letters the name holds after plant = "stem";.

This is the second file. It is not the file you keep. The file you keep has one storing line and one writing line, and its screen is leaf. Save this second file as Mint.java when you want to see the word change, then put the single storing line and the single writing line back.

the name changes between two lines

java
public class Mint {
    public static void main(String[] args) {
        String plant = "leaf";
        System.out.println(plant);
        plant = "stem";
        System.out.println(plant);
    }
}

In the same folder, 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 of the screen is stem. Two lines. leaf is on top because the first System.out.println(plant); runs while plant still holds leaf. stem is under it because the second System.out.println(plant); runs after plant = "stem";. The name plant is on neither screen line. If both screen lines say leaf, the second storing line is missing or still says "leaf". If both say stem, the first storing line already says "stem", or the first writing line sits below the change. The screen follows the order of the lines in the file. It does not keep an older word once a later line has stored a new one, and it does not skip ahead to stem before that storing line has run.

Do not write String a second time for the same name. String plant = "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. The useful fact is that javac wrote a line, so you do not type java Mint yet. The repair is to delete the second String and leave plant = "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 at all. After you have seen leaf and then stem, put that file back: one String plant = "leaf"; and one System.out.println(plant);.

Quotes around the name write the name

The writing line in the file you keep is System.out.println(plant); with no quotes. If you put quotes around the name, the line is System.out.println("plant");. Those quotes do what quotes did on the last page. They mark letters for the screen. The letters between them are p, l, a, n, t. Java does not look up the name, because a quoted word is not a name. The name plant can still hold leaf on the line above, and the screen still shows plant. The stored word leaf is not written, because the writing line never used the name. It used a quoted word that happens to be spelled like the name.

This is the third file. It is not the file you keep. Save it long enough to see the single line plant, then take the quotes off the writing line again.

the name written in quotes

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

Type javac Mint.java. The file is fine, so javac writes no line. Quotes around plant are allowed. They do not make javac fail. They also do not make the screen show the stored word. Then type java Mint. The screen shows plant. One line. No quotes. Not leaf. Not stem. If you wanted leaf, the writing line has to be the bare name System.out.println(plant);. The quotes belong on the storing line, around leaf, not on the writing line around the name. A space inside those storing quotes would be stored too. String plant = "leaf "; with a space after the f would make System.out.println(plant); write leaf with a space after it. The file you keep has no space there. The screen of the file you keep is leaf, four letters, and then the line ends.

The name has to exist first

Java reads the file from the top. The line that creates the name has to be above the line that uses the name. If you put System.out.println(plant); first, and only then write String plant = "leaf";, the writing line names plant before plant exists. 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. The repair is to move the String line back above the writing line. Order in the file is the order Java reads. A name cannot be used on an earlier line than the line that creates it.

This fourth file is the broken order. It is not the file you keep. Save it only until javac writes the error, then put the String line back on top.

the name used before it exists

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

Type javac Mint.java. javac writes an error. Do not type java Mint. You do not need the exact words of the error. The useful fact is the same as on the last page when the quotes were missing: javac wrote a line, so the file is not fine. Move String plant = "leaf"; above System.out.println(plant);. Save. Type javac Mint.java again. Now javac writes no line. Then java Mint shows leaf. The name leaf, with no String leaf line anywhere, is still an error, and it is a different error from this one. System.out.println(leaf); asks for a name called leaf. This file never creates that name. The name it creates is called plant, and the word stored in it is leaf. The word and the name are not the same letters. Putting quotes back around leaf on the writing line, System.out.println("leaf");, would also make javac quiet, but that returns you to the previous page. This page keeps the bare name on the writing line and keeps the quotes on the storing line.

You can now store a word in a name and write that word without putting the word on the writing line. String plant = "leaf"; stores leaf. System.out.println(plant); writes leaf and ends the line. A later plant = "stem"; changes the stored word, and a later println of the same name writes stem. Quotes around the name write the letters of the name instead. The creating line has to sit above the first line that uses the name. The next page reads a word that you type while the program is running, and stores that word in a name, so the screen can show letters that were not written between quotes in the file.

The screen is the word the name holds

The file you keep writes leaf. The name on the writing line is plant, and plant is not on the screen. The quotes are on the storing line, around leaf. One println is one line. Changing the stored word between two println lines changes the second screen line only.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

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

❓
7
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