Loading...

Java First Program

You already ran this file. The screen showed mint. This page stays with that same file and asks a different question: which line inside the file wrote that word, and which lines wrote nothing. You are not installing Java again. The version lines from the last page can stay as they were. Open Mint.java and read it from the top. The screen you are explaining is still one line, and that line is mint.

The file has more than one line, so a first guess is that each line of the file becomes a line on the screen. That guess is wrong, and the rest of this page shows why. Three of the lines name something. Only one of them writes. The braces group those lines. They do not write either. When you can point at the writing line with the file in front of you, this page is done. The next page adds a note the screen does not show. It does not change mint.

the whole file

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

The first line names the file

The first line is public class Mint. Read it as three words with a job, not as a sentence the screen will print. public means other code is allowed to use this file. You do not see the word public on the screen. class means this file is defining one named unit of Java. You do not see the word class on the screen. Mint is the name of that unit. The file on disk is Mint.java because this name is Mint. Change the name on this line and the file name has to change with it, or javac will refuse the file. That rule is about the file name. It is not about the screen. Look at the screen from the last page again. The screen does not show Mint. It shows mint. The capital M and the small m are not the same word. The first line never writes its name onto the screen.

The character after that line is an opening brace, {. The matching closing brace is the last line of the file, }. Everything between those two braces belongs to class Mint. The braces are fences. They tell Java where this unit starts and where it ends. A fence is not text for the screen. You will not see { or } next to mint. If you delete the opening brace, the file is broken and javac writes an error instead of staying quiet. That error is not this page. This page keeps both braces, and the screen stays mint.

The main line is where Java starts

The next line is public static void main(String[] args). This is the line people point at when they are asked where the program starts, and they are right about the start. They are wrong if they then say the screen shows main. Java looks for a line with this shape when you type java Mint. It starts running at this line. Starting is not the same thing as writing. The words on the line are instructions about how to start. They are not words for the screen.

Take the words one at a time, and check each one against the screen. public again means the line can be started from outside the file. static means Java can start this line without first building an object. You have not met objects yet, so the only fact you need is that the word static is required on this line and that it is not printed. void means this line does not hand a value back to a caller. There is no caller on this page, and void is not on the screen. main is the name Java looks up. The name has to be main, with those four letters, or java Mint will not start this file. The screen still does not show those four letters. String[] args is a place for words you might type after the program name. You did not type any extra words. You typed java Mint. args is empty for this run, and neither String nor args appears on the screen.

So the main line can be true in two ways at once. Java did start there. The screen did not receive a single word from that line. If your eyes rest on main because it sits in the middle of the file, move them. The word you remember from the screen is mint, and mint is not written on this line.

The inner line writes mint

Inside the main line, between its own braces, there is one more line. That line is System.out.println("mint"). This is the line that writes the screen. It is indented in the file so you can see that it sits inside main. The indent is spaces. Spaces are not written on the screen either. Java runs this inner line because it is inside the line where the program starts. The screen uses what this inner line writes. It does not use the name of the outer line.

Read the inner line from the left. System.out is the screen, the place lines are written. println means write this, then end the line. The part in parentheses is what gets written. Inside the parentheses the word mint sits between double quotes. The quotes mark mint as text. They are not part of the text. Take them away in your head and the word that remains is mint, and that is the whole screen. Leave them in the file. Without the quotes, javac does not treat mint as text. The semicolon at the end finishes the line for Java. You do not see a semicolon on the screen. You see mint, and then the line ends because println ended it.

Here is that writing line on its own, so the other lines cannot hide it.

only this line writes the screen

java
System.out.println("mint");

Nothing in the command java Mint chooses the word mint. The command only names the file. The word comes from the quotes. If the quotes held a different word, the screen would show that word instead, and this page does not do that. The quotes on this page hold mint, so the screen is mint. One line. No second line. No quotes on the screen. No word main. No word Mint.

📊 Which line the screen shows

The line The screen
public class Mint nothing from this line
public static void main(String[] args) nothing from this line
System.out.println("mint") mint

What people misread

The usual miss is to read the file from the top and assume the most familiar word is the output. main looks important, and it is important, because Java will not start the file without it. Importance is not output. You can cover the main line with a finger and the writing line is still System.out.println("mint"). You cannot cover the writing line and still explain the screen. Another miss is to say the screen shows Mint because the file and the class are named Mint. The screen uses the word between the quotes, and that word is spelled with a small m. A third miss is to count lines. The file shows several lines and several braces. The screen shows one. Braces, the words public and static and void, the name main, and the name Mint are all still in the file after the run. The run does not delete them. It also does not print them.

You can check this against the run you already did. javac Mint.java wrote no line. That silence means the file was accepted, including the main line and the braces. java Mint wrote mint. That one word is the println line doing its job. If you run java Mint again now, from the same folder, the screen is mint again. The file has not changed on this page, so the screen cannot change.

You can now put a finger on System.out.println("mint") and say that this is the line the screen comes from. You can put a finger on public static void main(String[] args) and say that Java starts there and that the word main is not on the screen. You can put a finger on public class Mint and say that this name matches the file and does not match the screen. The next page puts a note in the file. The note is for a person reading the file. The screen will still be mint, because the note is not a second println.

The screen comes from one line

The screen shows mint. System.out.println("mint") writes that word. The quotes are not on the screen. The line with main is not on the screen. The first line, public class Mint, is not on the screen. There is no second line.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

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

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