As I am sure you will all know, Java is a programming language which is heavily based on C and C++. Java is somewhat a portable language, it can be run on most machines regardless of the operating system or hardware it uses. Obviously there are some limitations to this, Java will be supported on operating systems which has a JRE installed on it (for more info see here: http://www.java.com/en/download/manual.jsp)
Java has mixed opinions, some people dislike it, others swear by it. However, most universities and colleges (in England at least) use Java. This means in a few years time, Java will be (almost indefinitely) the programming language which nearly everyone is using.
To program in Java, you will require a few things to get started. Firstly you will require the JDK (Java Development Kit) (found here: http://java.sun.com/javase/downloads/index.jsp). You will also require the API for the language (which can be found here: http://java.sun.com/javase/reference/api.jsp)
Now, this is enough to get you started, however writing programs in notepad and then using a CLI to run your programs gets a bit annoying after about 5 minutes. I suggest using a program called Eclipse, it will help identify errors in your code and also run/compile your program for you, (Eclipse can be downloaded for free here: http://www.eclipse.org/downloads/downlo ... -win32.zip For other operating systems please see http://eclipse.org)
Now we have everything we need to get started. Once you have installed everything you can begin making your first program!
First off, you will need to run Eclipse, once it has started you will be presented with a Dialog box asking you for your workspace. I will have my workspace as
but you may wish to have yours as something else. Once you have selected your work space, you will be presented with a welcome screen, Close this (there is a tab on the top left side of the screen saying "Welcome" and then a X next to it, press the X). Now you have the eclipse screen. We can now begin programming.C:\Users\Joe\Documents\Swivvet\Tutorials\Java
The first thing we need to do is make a new Java project. This can be achieved by going to File->New->Java Project. You will then be presented with a screen similar to this:
I have named my project "Hello World" and then pressed Finish.
My Project then appears on the left side of the screen where I can sell all my files associated with it. At the minute we have the JRE System Library and a src folder. We are interested in the src folder.
First we need to add a class to our project. This can be achieved by going to File->New->Class
You will then be asked for different information, we are interested in the class name field. I have named mine Hello.
Eclipse has now generated this code for me:
- Code: Select all
public class hello {
}
This is our class, everything we write will go inside the curly braces. At the minute this program won't do anything, it hasn't been told to!
In Object Oriented Languages there is something called a "constructor" and this is like the blue prints to a program. So we need to write our constructor.
This is done fairly easily:
- Code: Select all
public class hello {
public hello(){
}
}
Notice the name of the constructor, it is the same as the class name, you MUST remember to do this otherwise you will have an error.
So now we have a constructor, but it has nothing in it. We are going to use Java to output a simple "Hello World" into the console. Java has something which will do this for us.
- Code: Select all
System.out.println("Hello World!");
Will do exactly that. Notice the semi-colon at the end? This must go at the end of every statement. System.out.println is already in the Java Library, so we do not need to write a special method to do this for us, we can use this one instead. The complete code at the moment will look like this:
- Code: Select all
public class hello {
public hello(){
System.out.println("Hello World!");
}
}
We could alternatively have used System.out.print("Hello World"); However, println prints "Hello World" on a new line, so if we were to have several of these statements each with a different message, println would output a much tidier result.
If you still run your program, nothing will happen, it still has not been told what to do.
To tell a Java program what to do, it has to have a main method. Every Java program has to have one of these.
The main method will look something like this:
- Code: Select all
public static void main(String[] arg0){
}
We could have wrote System.out.println("Hello World!"); inside the main method, and everything would have worked fine, however for the purpose of this tutorial, I decided to use a constructor.
Running the program still won't work, we have a main method but it doesn't know what to do.
This is where our constructor from earlier comes in.
- Code: Select all
public static void main(String[] arg0){
hello myHello = new hello();
}
The
- Code: Select all
hello myHello
means that I am making an instance of the class hello (the first bit) called "myHello" this means if I were to have multiple methods or variables in a program, I could access them in different classes using myHello.methodName(); or something similar to this.
The
- Code: Select all
= new hello();
is the bit which runs our constructor, now if we run the program, we should see a result.
To run a program in eclipse it is fairly simple, you need to navigate to the following screen:
and select Java Application.
Once you have done this, you should see the result at the bottom of the screen in the "Console Window"
My code outputs Hello World2! because I changed the source code. Yours should output whatever you want it to.
Please post any questions below.
Thanks