Writing your first Java program
Hello Java is a tutorial to help you set up a development environment and write your first Java program! In programming, “Hello World” tutorials like this are commonly used to introduce people to new programming languages.
*You could also use a text editor like Sublime Text 2 or Notepad++ and build and run your program manually, but that will not be covered in this tutorial.
After Eclipse launches, there will be a welcome tab open. Feel free to read an explore it, and then close it by clicking the x in the corner of the “Welcome” tab. You will want to create your first “project”. One way to look at a project is as a collection of files that make up a program. Create this by clicking “file->new->Java Project” from the toolbar. In the project name field type “Hello Java” and click finish.
In the left panel called the “Package Explorer” you should see your project. In that project will be two icons, one saying src, and one saying JRE System Library. Src is where all of your source code files will live. JRE System Library is the standard Java library that provides the basic classes and methods that you need to write a Java program.
Right-click on “src” and select “new->class”. In the name field type “HelloWorld”. Convention says that class names should start with a capital letter and the first letter of proceeding words should be capitalized. Spaces are not allowed. Find the question “Which method stubs would you like to create?” select the first box “public static void main(String[] args). Also select “Generate comments” under the question “Do you want to add comments?”. Click finish.
Now let’s break it down chunk at a time.
Lines 1-4 are an example of comments. Comments are parts of source code files that are not instructions for the computer. In Java, there are 3 different kinds of comments. Block comments are the first kind, and they start off with /* and end with */. Each new line of a block comment starts with *. The second are “Javadoc” comments. These are similar to block comments, but they start off with /**. These are special comments used to create public documention that other users can see. In your Eclipse window, hover your mouse over “String” on line 10 to see what I mean. The pop-up information about String is written in Javadoc comments in the source code for String.
Line 11 is an example of inline comments. Everything on a line following // will be considered a comment and the comment ends when a new line is started.
Line 5 is the class declaration. “public class Helloworld” is saying “This file will describe a class called HelloWorld which will be publicly visible to other classes”. Visibility and “scope” is a more advanced topic and will not be covered here.
Lines 10-13 declare a method called “main”. Again, public declares the visibility of the method. Static means that this method can be called without needing an instance of the class HelloWorld. Void means that this method will not return anything. If it said “int” or “String” instead, this method would need to return a value of the declared type at the end. Main is the name of the method. Main is a special type of method in many programming languages, including Java, and is the name of the method that executes when a program is ran. String[] defines the type of the argument to be an array of Strings. An array is just a collection of objects of a certain type, in this case Strings. Args is the name of the [argument]((helloglossary.html#argument) which is a value passed in to the method when it is called.
“System.out.println(“Hello World!”);
This line is representative of how Java Programs are written. Let’s break it down.
System is a class in the Java System Library that all programs have access to. Methods are invoked on a class by following the Class name (or the object name if you have created an instance of it) with a . and then typing the name of the method you want to call. In this case we want to call the method out.
“out” is a static method that takes no arguments and returns an object of type PrintStream. This is what will allow us to print something out. Remember, static means that you can access it without creating an instance of it.
“println” is an instance method of PrintStream that takes a String as an argument. Arguments are provided between () directly following the methods name. This method will print the string to the console. This will be shown later.
Lastly, all statements in Java like the line above must end with a semi-colon.
In summary: System.out.println(“HelloWorld”); gets an instance of PrintStream from the System class by calling the out method, then calls println with the argument “HelloWorld” to print that string to the console.
At the bottom of the Eclipse window should be a section that says “Console” with the words “Hello World” printed in it like the picture below.