Sub topic 7
Write and Save HelloWorld.java
A Java program starts as text that you write and save in a file. In this chapter, you will create that file for a small greeting program. You will also learn how its parts fit together.
You need a place to save files and a plain text editor. You do not need to run any Java commands in this chapter.
What is a Java source file?
A program is a set of instructions for a computer. Source code is the text a person writes to describe those instructions.
A Java source file is a plain text file that contains Java source code. Its name ends in .java. The part after the last dot is called the file extension. An extension helps identify the kind of file.
For this program, the file name will be:
HelloWorld.java
Later, the Java compiler will read this file. A compiler is a tool that translates source code into a form Java can run. For now, your job is to write and save the source file.
Choose a folder and an editor
A folder is a place where you keep files. Choose a folder that you can find again. If you already chose a folder for your Java work, use it here. Otherwise, create a folder named JavaPractice inside your Documents folder.
You also need a plain text editor. This is a program that saves text without adding styles such as bold text, font choices, or page layouts. The Java compiler needs the actual source text, not a document with hidden formatting.
You can use one of these:
- Windows: Notepad or a code editor.
- macOS: TextEdit in plain text mode or a code editor.
- Linux: a plain text editor provided with your system or a code editor.
A code editor is a text editor designed for writing programs. It may color parts of your code to make them easier to read. Those colors are for display; they are not part of the Java file.
Avoid saving the program as a document from a word processor. A file named HelloWorld.java must contain plain text, even if its name looks correct.
Enter the program
Open a new, blank file in your editor. Type or copy the following program into it:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}The spaces at the start of some lines are called indentation. They make the structure easier to see. Java does not require these exact spaces, but matching the example is a good habit.
Check the spelling, capital letters, and punctuation carefully. In Java, HelloWorld and helloworld are different names. This is called case sensitivity: uppercase and lowercase letters matter.
The program is intended to display Hello, World! when it runs. Saving the file does not run it; that comes later.
Save the file
Choose your editor’s Save or Save As option. In the save window:
- Open your chosen Java work folder, such as
JavaPractice. - Enter the file name
HelloWorld.java. - If the editor asks for an encoding, choose UTF-8. Encoding is the way text is stored as bytes; UTF-8 is a common choice.
- Save the file.
The name must have a capital H and a capital W, with no space between the words. It must end in .java, not .txt or .docx.
Some editors add an extension for you. Watch for a warning or a setting that would add .txt. On Windows, Notepad may offer a “Save as type” choice; if so, choose “All Files” before saving. On macOS, if TextEdit offers to keep .java or change it to .txt, keep .java.
After saving, look at the editor’s window title or tab. It should show HelloWorld.java, not an untitled document. Also remember the folder you chose. You will need to find the file again.
Read the program from the outside in
You do not need to memorize every word yet. First, look at the program as a set of parts inside other parts.
The class
The first line begins with public class HelloWorld.
A class is a named container for Java code. This class is named HelloWorld. The word public makes the class available to other code.
For this public class, the source file must be named HelloWorld.java. The class name and the file name match, including their capital letters. The file name adds the .java extension.
The opening brace { at the end of the line starts the class body. The last closing brace } ends it. Everything between those braces is inside the class.
The main method
Inside the class is this line:
public static void main(String[] args) {
A method is a named set of instructions. This method is named main. For this program, main is the starting point that Java uses when the program runs.
The words and symbols on this line each have a job:
| Part | Meaning |
|---|---|
| `public` | Allows Java to access the method from outside the class. |
| `static` | Lets Java call the method without first creating an object from the class. An object is a value made from a class. |
| `void` | Says that the method does not return a value when it finishes. |
| `main` | Names the method that Java uses to start this program. |
| `String[] args` | Provides a place for text values passed to the program when it starts. |
A string is text, such as a word or sentence. The square brackets in String[] indicate an array, which is a group of values in a set order. Here, args is the name given to that group. You do not need to pass any values to this program.
The opening brace at the end of the main line starts the method body. The closing brace on the next-to-last line ends it. The print instruction sits inside those braces.
The print instruction
Inside main, the program has one instruction:
System.out.println("Hello, World!");
System.out.println tells Java to write text to its usual output, which will normally be your terminal. A terminal is a window where you can enter commands and see their results. The println part also moves to a new line after the text.
The parentheses contain what to print. The double quotation marks show where the text starts and ends. The marks themselves are not part of the greeting.
The semicolon ; marks the end of this Java statement, or instruction. Keep it after the closing parenthesis.
Use straight double quotation marks, as shown in the example. Curly quotation marks may look similar, but Java will not treat them as the same characters.
Check the braces and punctuation
The program has two pairs of braces:
- The outer pair surrounds the class.
- The inner pair surrounds the
mainmethod.
Each opening brace needs a matching closing brace. The indentation helps show which closing brace belongs to which opening brace. The two lines inside the class are indented, and the print instruction is indented one level further.
Also check these small but important details:
Stringstarts with a capitalS.Systemstarts with a capitalS.outandprintlnuse lowercase letters.- The text is between double quotation marks.
- The print instruction ends with a semicolon.
- There are two closing braces at the end of the file.
A character that is missing or changed can make the program invalid. If something looks wrong, compare your file with the complete example above, one line at a time.
Make sure the saved file is the right one
Use your file browser to open the folder where you saved the program. A file browser is the app you use to look through folders and files. Find HelloWorld.java.
Some systems hide file extensions. This can make HelloWorld.java.txt appear to be named HelloWorld.java, even though its actual extension is .txt.
To check:
- On Windows, turn on file name extensions in File Explorer’s View options. In Windows 11, look under View, then Show, then File name extensions.
- On macOS, select the file in Finder and choose File, then Get Info. Check the name and extension shown there. Finder also has a setting to show all file extensions.
- On Linux, use your file browser’s properties or view options to check the full name. The exact menu depends on the browser.
If the actual name is HelloWorld.java.txt, rename it to HelloWorld.java. If your system asks whether to change the extension, confirm the change to .java. Then open the file in your text editor to make sure it still contains the program. Changing an extension changes a file’s name; it does not turn a formatted document into plain text.
If you open the file and see an older version, check whether you saved another copy in a different folder. Edit the copy in your chosen Java work folder. After making changes, save it again.
Final check
Before moving on, make sure that:
- Your file is plain text and is saved as
HelloWorld.java. - The file is in a folder you can find again.
- The class is named
HelloWorld, with the same capital letters as the file. - The complete program matches the example, including braces, quotation marks, and the semicolon.
- You have saved any changes made since you first created the file.
The file is now ready for the later step of compiling and running it.
Exercise
A file browser shows the full name HelloWorld.java.txt. What should the name be for this public class?
Show answer
HelloWorld.java. The extra .txt must not be part of the file name.
Exercise
What do the two pairs of braces surround in the example program?
Show answer
The outer pair surrounds the HelloWorld class. The inner pair surrounds the main method.
Exercise
Open your saved file and change the greeting to Welcome!. What else must stay around that text, and what should you do after editing?
Show answer
Keep the straight double quotation marks around Welcome!, keep the rest of the print instruction, and save the file.