Sub topic 7
Write and Save HelloWorld.java
A Java program starts as source code: instructions written as text for Java to read. In this chapter, you will put those instructions in a file named HelloWorld.java.
The name matters. Java expects the file name to match the name of the public class inside it. A class is a named unit that groups Java code and data. Here, the class is named HelloWorld.
Choose a folder and a text editor
Create or choose a folder where you can find your work. For example, you could make a folder named JavaPractice in your documents folder. You will use this folder again when you compile and run the program.
Use a text editor, which is a program for creating and changing plain text. Plain text stores characters without document formatting such as fonts and colors. Do not use a word processor that saves formatted documents, because its file may contain extra information that is not Java code.
- On Windows, you can use Notepad.
- On macOS, you can use TextEdit. Choose Format > Make Plain Text before typing.
- On Linux, you can use a plain-text editor supplied with your system.
Enter the program
Open a new, empty document in your text editor. Type or paste this code exactly:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}Check the capital letters, quotation marks, and punctuation. Java treats uppercase and lowercase letters as different. For example, HelloWorld and helloworld are not the same name.
The spaces at the start of some lines are called indentation. They make the code easier for people to read. Java does not require this exact indentation, but using it helps you see which lines belong together.
Save the file
Save the document in your chosen folder as:
HelloWorld.java
A file extension is the ending of a file name, such as .java. It helps identify the kind of file. Make sure the name is not HelloWorld.java.txt or HelloWorld.java.rtf.
- In Notepad, choose File > Save As. Enter
HelloWorld.java, choose All files as the file type, and save it in your chosen folder. - In TextEdit, save the plain-text document as
HelloWorld.java. If asked whether to use.txt, keep.javainstead. - In another editor, use Save As and check the full file name before saving.
If your editor asks for an encoding, which is a way to represent text as bytes, choose UTF-8.
Some file browsers hide file extensions. That can make HelloWorld.java.txt look like HelloWorld.java. You can check the full name in a terminal, which is a window where you enter commands.
Open the terminal in your chosen folder, using the folder-navigation steps from earlier. On Windows, enter:
dirOn macOS or Linux, enter:
lsThese commands list the files in the current folder. Look for HelloWorld.java in the list. If it is missing, check that you saved the file in the same folder the terminal is showing.
Read the code
You do not need to memorize every word yet. For now, follow the parts that give the program its shape.
The class
The first line starts a public class named HelloWorld. The word public makes the class available from outside itself. For this program, the public class name and file name must match: HelloWorld in the code and HelloWorld.java on disk.
The curly braces, { and }, mark the start and end of a section of code. The outer pair surrounds the class.
The main method
A method is a named set of instructions. Java looks for the main method when it starts this program.
The words on its first line have specific jobs:
publicmakes the method accessible to Java when it starts the program.staticlets Java call the method without first creating an instance of the class.voidmeans the method does not return a value.String[] argsholds any text inputs supplied when the program starts. This program does not use them.
The inner pair of curly braces surrounds the instructions in main.
The output line
System.out.println tells Java to print a line of text to the usual output, typically the terminal. The dots connect the parts of that name.
"Hello, World!" is a string, or a piece of text. The double quotation marks show where the text starts and ends. The semicolon at the end of the line marks the end of the instruction.
Make a final check
Before moving on, confirm that:
- The file is named exactly
HelloWorld.java. - The class name in the file is exactly
HelloWorld. - The code has both pairs of curly braces.
- The greeting has double quotation marks, and the print line ends with a semicolon.
- The file is saved as plain text in the folder you can find from the terminal.
You now have a Java source file. The next step is to compile and run it.
Exercise
Your editor saved the file as HelloWorld.java.txt. What should the file be named, and why?
Show answer
Name it HelloWorld.java. The .java extension identifies the Java source file, and HelloWorld matches the public class name.
Exercise
What text will the println instruction print?
Show answer
Hello, World!