Introduction
In this tutorial, we will look at how to write a simple application that prints out text to the screen.
The Program
To get started with Java, we will make the standard first program - one that prints out "Hello World!" to the screen. Create a new file called HelloWorld.java and place the following code in it. Remember that a .java file must contain only one class, and that class has to have the same name as the file.
// Place this code in a file called HelloWorld.java public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } }
Now let's look at this one line at a time. The first line says:
public class HelloWorld
We can see from this line, and the curly braces ('{' and '}') below it, that our program is contained within what is called a 'class'. We will learn more about classes in another tutorial. Basically, though, classes are simply containers that represent objects in real life. They hold data (any kind of information) and operations or methods(what you can do to the data).
Notice that before the keyword 'class' is the keyword 'public'. This is called an access modifier. We will talk more about these in another tutorial as well. But by listing the class as public, we indicate that the class can be seen by any one. It's public!
below the class definition, we have the line that says:
public static void main(String[] args)
This line is our first glimpse at what is called a method in Java. In some other programming languages, they are called functions, or subroutines. Like we discussed earlier, a class can contain operations or methods. This line of code shows us that there is one method in our class called main.
To create a method, we need to state a few things. First of all, we need to state its access modifier. In our case, we are using the public modifier, to show that this method can be used by anyone, just like the class.
The next thing we have when we create our method is the word static. This may be a little confusing at first, since we don't really have the background necessary to explain it well. We'll talk about it more later, but for now we'll just mention that it means the method belongs to the class, rather than an instance of the class.
A method's job is to perform a particular task. Usually, we want the method to do something, like a mathematical computation, and then give us the result. Each method has the ability to return a value back to the place that called it. The value could be a number, a word, true or false, or a complex structure - just about anything. It can also not return anything. In our program, we simply want to write out a word to the screen. We don't need to return anything back. To indicate that we don't want to return anything, we use the keyboard void. We will look at other return types later.
The next thing on the line is the word main. This is the name of our method. Methods can be called whatever you want, as long as you follow Java's rules for naming. Well talk more about this later as well.
After the name of our method, we have a couple of parentheses, with the words String[] args in the middle. Each method can be given some data to work with. These data are called parameters. Our list has one parameter, which is called args and has the type String[]. The String type is basically words or text. the [] indicates that the parameter is an array (the word in Java for list) of them. So we're giving our main method a list of words to use. It turns out that our method is going to completely ignore this parameter….
In order to run a program in Java, we need to have a class that contains a method that looks like the method we've defined in our program:
public static void main(String[] args)
When we run the program, it goes looking specifically for this method, so be sure to have this method somewhere in your program!
Now, the last important line in our small program is the line that says:
System.out.println("Hello World!");
System is another class that is defined in Java. We are going to use one of the class's data objects, called out, which has a method called println. This method, println prints out a line of text. Then in the parentheses, we give it a parameter - in this case, what we want to print out. We give it the String (Java's word for text) "Hello World!".
We can now compile and run our program, and we will get the following output:
Hello World!
Conclusion
We've seen how to write a very simple Java program. But we're really just getting started. Some of the directions we might want to go to next is to learn about data types in Java, and to do some more with methods in Java. These are the two building blocks in a Java program.
Exercises
- Modify the code so that it prints out the text "Goodbye world!" instead.
- Change the name of the main method and look at the error that occurs when you try to run the program. Why does this error occur?
![]() |
Having problems with this tutorial? Try the troubleshooting page! |