Last time we looked at setting up Eclipse and installing the JDK
This Tutorial
In this tutorial we'll learn to make a new project and start to code! We'll be making the famous "Hello World" program which, as the name suggests, simply prints out Hello World
Setting up
Open Eclipse if you haven't already, then go to File > New > Other. Search for Java and choose the "Java Project" option as below
Type "Hello World" as the name then click "Finish"
You may be asked to change perspective, click Yes to this
On the left you should now have a Hello World folder. In here you'll have a "src" folder, this is where our code is going to be.
In Java you need to have what's called a "Class" for each bit of code. The program will start at the "main" class so we need to make that now.
Go to File > New > Class
Type "com.helloworld" as the package name (we'll cover what this is later) and "main" as the Class name. Be sure to tick the box as shown in the picture below
The Code
You should now have a few lines of code. The first line is the package name. This needs to be declared at the top of each Class
Next we have the class declaration. Note that the rest of the class is wrapped within curly braces, that's to show it's part of the class.
Similarly, we have what's called the "main" method. A method is block of code which executes when told to. The contents of a method are wrapped in curly braces so the computer knows it's all one method.
Don't worry if you're confused by all the terminology, for now we can just focus on where it says
// TODO Auto-generated method stub
Remove this line and replace it with
System.out.println("Hello World!");
Then run the program by going to Run > Run
Choose "Java Application" and hit OK. You should see "Hello World!" appear at the bottom of the screen like so:
Well done! You've made your first program. As you may expect, if you change the Hello World text in the blue (line 6 in my screenshot) then it will print whatever you replace it with. Have a play around and be sure to come back for more next time!
No comments:
Post a Comment