Friday, September 20, 2013

Java (Programming Language) For Beginners


Step 1: What Is java?

          Java is just one of the hundreds of different programming languages in the world. Java language is an object-orientated programming language which was developed by Sun Microsystems. Java programmes are platform independent which means they can be run on any operating system with any type of processor as long as the Java interpreter is available on that system.

Step 2: What You Will Need

            You will need the Java Software Development Kit from Sun's Java site. Follow the instructions on Sun's website to install it. Make sure that you add the java bin directory to your PATH environment variable. To find the Java Software Development Kit, go to the top right-hand corner of the screen and you will see a search bar. Type in: Java Software Development Kit. The the search results appear, find the one that says something along the lines of download.

Step 3: Writing Your First Java Programme:Part 1

              You will need to write your Java programs using a text editor. When you type the examples that follow you must make sure that you use capital and small letters in the right places because Java is case sensitive. The first line you must type is:

public class Hello

This creates a class called Hello. All class names must start with a capital letter. The main part of the program must go between curly brackets after the class declaration. The curly brackets are used to group together everything inside them.

public class Hello
{

}

Step 4: Writing Your First Java Programme:Part 2

              We must now create the main method which is the section that a program starts.

public class Hello
{
public static void main(String[] args)
{

}
}

You will see that the main method code has been moved over a few spaces from the left. This is called indentation and is used to make a program easier to read and understand.

Here is how you print the words Hello World on the screen:

public class Hello
{
public static void main(String[] args)
{
System.out.println("Hello World");
}

Step 5: Writing Your First Java Programme:Part 3

             Make sure that you use a capital S in System because it is the name of a class. println is a method that prints the words that you put between the brackets after it on the screen. When you work with letters like in Hello World you must always put them between quotes. The semi-colon is used to show that it is the end of your line of code. You must put semi-colons after every line like this.

Step 6: Compiling The Programme

              What we have just finished typing is called the source code. You must save the source code with the file name Hello.java before you can compile it. The file name must always be the same as the class name.

Make sure you have a command prompt open and then enter the following:

javac Hello.java

If you did everything right then you will see no errors messages and your program will be compiled. If you get errors then go through this lesson again and see where your mistake is.

Step 7: Running The Programme

           Once your program has been compiled you will get a file called Hello.class. This is not like normal programs that you just type the name to run but it is actually a file containing the Java bytecode that is run by the Java interpreter. To run your program with the Java interpeter use the following command:

java Hello

Do not add .class on to the end of Hello. You will now see the following output on the screen:

Hello World

Congratulations! You have just made your first Java program.

No comments:

Post a Comment