First Java Program
Navigate Getting Started topic:
)
|
In order to write your first Java program, follow the instructions below:
1. | Proceed only if you have successfully installed and configured your system for Java. | ||
Read our guide on installing the Java platform. | |||
2. | Open your preferred text editor – this is the editor you set while installing the Java platform. | ||
For example, Notepad or Notepad++ on Windows; Gedit, Kate or SciTE on Linux; or, XCode on Mac OS, etc. | |||
3. | Write the following lines of code in a new text document:
|
||
4. | Save the file as FirstProgram.java – the name of your file should be the same as the name of your class definition and followed by the .java extension. This name is case-sensitive, which means you need to capitalize the precise letters that were capitalized in the name for the class definition. | ||
5. | Next, open your preferred command-line application. | ||
For example, Command Prompt on Windows; and, Terminal on Linux and Mac OS. | |||
6. | In your command-line application, navigate to the directory where you just created your file. If you do not know how to do this, consider reading through our crash courses for command-line applications. | ||
7. | Compile the Java source file using the following command which you can copy and paste in if you want:
javac FirstProgram.java |
||
8. | Once the compiler returns back to the prompt, run the application using the following command:
java FirstProgram |
||
9. | The above command should result in your command-line application displaying the following result:
Hello World! |
[edit] Did the program execute correctly?
- Ask for help if the program did not execute properly in the Discussion page for this chapter.
If the program does not work as you expect, check the following common problems:
- Are you sure all words are spelled correctly and with the exact case as shown?
- Are there semicolons and brackets in the appropriate spot?
- Are you missing a quote? Usually, modern IDEs would try coloring the entire source as a quote in this case.
- Are you launching the javac or java programs correctly?
- Did you navigate to the program's location in the command prompt using the CD (Change Directory) function?
[edit] Analysis of your first Java program
In the code for your first Java program, you created a class called FirstProgram
which serves as a virtual abstraction model for our program. You could have named this anything but we chose the name FirstProgram
in order to highlight the fact that this abstraction serves as the very first program we create in Java.
![]() |
public class FirstProgram { }
|
Notice how we placed the public
access modifier next to the name for the class definition. This tells us that the class is accessible by the Java environment it exists within. In our case, this environment is the Java Runtime Environment (JRE) itself, which would have direct access to the FirstProgram
class and all its public
member properties, fields and methods.
![]() |
public class FirstProgram {
public static void main(String[] args) { } } |
Therefore, when we declare the main(...)
method, we make sure that this method is accessible to the JRE using the public
access modifier. This particular method has some interesting features associated with it. We can see that the method itself returns nothing (as is indicated by the void
keyword) but the method accepts arguments args
of type String[]
. The square brackets [
and ]
tells us that this the args
argument is a list of type String
, therefore instead of just one String
, this variable can hold several String
elements. Such variables are called arrays.
Finally, within the body of the main method, we have written a single line of code:
![]() |
System.out.println("Hello World!");
|
This command is called a print statement and is used to print out or display stuff on the command-line application interface. It's a slightly complicated routine to write the whole statement but you get used to writing it in your programs. We will be explaining this line and other System
commands in a later chapter.