Martrinex Learning Center
HOME CONTENTS
 
This website is underconstruction.
Author: Martin Sykes
Challenge: I have challenged myself to 1 article per day, and framework improvements! Enjoy
Challenge end: unknown

Hello World Application

Now for your first Java application, this will be a simple Hello World message to show you how to create a program, where the code goes and how to write to the console.

Create your project

1. Open "eclipse.exe"
2. Use any workspace
3. Create a new project (File->New->Java Project)
4. Name it "HelloWorld" (note its CaSe SeNsItIvE)
An empty project has now been created.

Add the main class

Time to use the project explorer, and create your first class, a class is a page where all the code for your program will be stored, because this class will be the first thing your program opens, it must be named the same as the program "HelloWorld", lets get on with it...


Right click on the "src" folder then click New->Class as in the image above.


Name it the same as the project "HelloWorld", still case sensitive, and finally tick "Public Static Main void", this gets Eclipse to create our first function, where every console program starts the "main function", out code will go within this function within the class we are about to create. Click "Finish"

Packages

Packages are folders within a project, it helps your split up larger projects both on the harddrive and in Eclipses workspace, for now on the smaller projects we will just use the default package. (default folder)

What happened?

The Class is created and added to the project explorer.
It is placed within the default package, which you may have noticed, eclipse discourages, but for small projects it doesn't matter.

The new file also opens automaticly lets take a look at what we have:

The first view of the code window, shows it colours and highlights code as epected in a proffessional IDE.
The "public class HelloWorld" was create which can store all our functions.
Also the main function was added to the class, this is where the program will begin.

Multiline comments go within: /* COMMENT HERE */ as show above with @param args
Single line comments go after 2 slashes eg: // COMMENT HERE as shown by TODO auto-generated...

Eclipse recognises TODO in comments as a todo list and gives is a little notepad on the right handside, also it creates a blue box on the left hand side as shown in the example, you will notice blue / yellow and red boxes being added to the left as you code, it will even get cluttered, any warnings / errors and notes are shown here and clicking on them simply scrolls the page down and highlights the code in question, handle when your code is 50 odd pages long.

Coding time!

Code: public static void main(String[] args) {

// TODO Auto-generated method stub
System.out.println("Hello World");
}
 

Simple enough:

System = the console area / commands
Out = short for output to write to the console area
PrintLn = tells it to print some text and finish with a new line
"Hello World" is a parameter for PrintLn telling it what to write.
Every line of code in C ends with a semi-colon ';' unless it is empty, or the line ends with a { or }(codeblocks)

Now run it!

Click the white and green play button shown in the image above,

Output

And vuala its there! hardly the classic DOS of the 1980's but it does its job.

Add Comment

Help the web master by commenting on the tutorials, thankyou.
Name:
Email: (this will not be shown)
Comment:
  Send email to admin, don't post.

 

Comments

Nobody has commented on this article.
Be the first, the webmaster needs comments good or bad to improve this site.



Copyright (c) 2008, Martin Sykes.
Learning Center is a branch of Martrinex Systems, Martrinex.net
Do not copy any materials from this site without permission from the auther.
Martrinex Learning Center[X]
  Introduction Beginner Intermediate

Important useful well commented source codes
Please read the import tutorial to know how to use them.

Source