We're going to start off by coding the traditional Java Hello World program!
Java programs are very simple to write once Eclipse is started up and you know how to work it. Turns out using Eclipse is simple, and we're going to walk through writing our first Java program this way.
Note: We're using Eclipse for these Java tutorials. If you've never heard of Eclipse, then you need to look back at the first tutorial. It's so easy to set up that it's worth it to use it as our Java editor with these tutorials.
Because we're using Eclipse, we need to create a Java project before we can begin coding. A Java project is just a bunch of Java files and other files needed for your program to work. Every time you want to work on something new, I recommend creating a new project for it, as it's a good habit to have. So, if you haven't already, go start up Eclipse, and hit ok when the Workspace Launcher box appears.
Right click on the left side panel, go to new, and select Java Project. (If you don't see that immediately, just hit Project... and you'll see more project types. Java Project should be at the very top, but if not, go to the folder that says Java, and you should see Java Project in there.)
Great! Now we have a bunch of options to set for our new Java Hello World project. All of these options can quickly become overwhelming, so once again we won't go into any details. Name your new project Hello World, leave everything else how it was, and then hit finish.
The form will close and you should see your new project for your Java Hello World program on the side panel.
If you expand your new project by clicking the plus sign next to the folder, you'll see a folder called src and also a JRE System Library. Again, don't worry about these things. Right click on the src folder, go to new, and select Class. All Java files are the same as class files, so that's usually the only option you'll ever select.
Now you have yet another form to fill. That's ok; just give this new file a name. We're going to call it HelloWorld, so type in HelloWorld (no spaces!) where it asks for the name. Java files should always start with a capital letter as the first letter and should have no spaces, so remember that. Also, check the box that says public static void main(String args), because you'll need that. Then hit finish.
Now you'll see your newly created file, and you'll see that some of the work has been done for you. Let's take a quick look at what it is you're seeing.
public class HelloWorld is the beginning of your class file. Notice it has the same name as the file itself. The name, including the capitalized letters, must match the file name in Java or it won't work. Eclipse does this work for you of course. Helloworld is not the same as HelloWorld, because they are not exactly the same. The second HelloWorld has a capitalized W, the first one does not.
public static void main(String args) is called the main method. I'm not going to discuss what methods are right now, but without this statement, you cannot run your program. So remember that!
You'll notice two open brackets like this one in front of two of the lines of code:
and then further down you see two closing brackets that looks like:
Anything you write inside the inner two brackets belong to the main method, and anything inside of the outer brackets belongs to the class. All methods and classes have opening and closing brackets, but we'll touch on that some other time.
You'll see "extra code" in different colors. Those are comments. Comments help you to remember what the code does and helps others who might read your code to understand what the code does. Nothing is worse than writing code and then coming to it later and forgetting what it all does!
See the above? This is a multiple line comment and can be ignored. You may delete this if you wish.
The line above is a single line comment, and it can also be ignored. You may also delete this line if you wish.
Now we're going to make the screen say hello to the world. To do this, make sure you're typing inside of the main method, meaning inside the open and close brackets belonging to the main method. Just write the new line under public static void main, because that would be considered to be inside the main method brackets. Add this line:
Your full Java Hello World code should look like this:
If something is wrong with what you typed, Eclipse will automatically underline the problems in red. If you get this, make sure everything is spelt correctly and that you are using correct capitalization. When there are no longer any red underlines, right click the text screen (anywhere in the box where you wrote your code), go to Run As, and select Java Application. If it tells you that you need to save the file, save it, and then watch it run. In the little console at the bottom of the screen, you should see it display Hello world! That means that the program works, and you've now written your first program in Java! Congratulations!
I put a red border around the output screen. That is where Java displays its output in Eclipse.
Congratulations, you have just finished your first Java program!
Now that everything is set up and works, you have enough tools at your disposal to create highly sophisticated programs. Yes our Java Hello World program was simple, but it is important for you to see what Java code looks like. The following tutorials will teach you all of the cool features Java has to offer, and of course if you have any questions please check out the forums and/or shoot me an email and I'll be glad to help!
The next lesson will cover what the line you inserted does as well as other cool commands. Remember if you have any questions, comments, or concerns, feel free to contact us. Until next time...
Leave the Java Hello World tutorial and return to Beginner Java Tutorials
Leave the Java Hello World tutorial and return to Java Made Easy!