Saturday, January 31, 2015

Hello World in Java

This post will help you to write your first Java program,the function of this program is to display Hello World on your command prompt.

Open Notepad And Type The Following

Open the notepad on your PC and type in the following code, don't worry if you don't understand it I have Explained every line below.

public class HelloWorld

  public static void main(String[]args)
   {

//Prints Hello Word

  System.out.println("Hello World");
  
   }

}

Saving the File


Now save the file as HelloWorld.java,remember to change the save as type to All Files otherwise  even though you add .java to the end the file will be saved as a .txt file so don't forget to change it.
Now that you have a .java file all you need to do is run the program,if you don't know how you can check my post on How to Compile And Run a Java Program.

Explanation

Line 1 (public class HelloWorld)

This line is where the class is declared, public class are keywords in the java library and HelloWorld is the name of the class.

Line 3 (public static void main(String[]args))

This line is declaring the main method , this method is what the JVM(Java Virtual Machine) will run, anything outside of this method will not work unless it has been referenced inside this method.The Key words have Meanings, these meanings can be found from the following site.


Line 8 (  System.out.println("Hello World");)

This line is part of the code that displays Hello World on the screen, This is an in-build method in java and don't forget the semi colon at the end it is the full stop in programming.This method can be used to print any form of text or numbers on the screen.

Other parts that are not explained

1.The parenthesis

    These represent the boundary of the method and class,that means the first parenthesis is opening the class and the second is opening the method third is closing the method and fourth is closing the class.

2.Comments
  
   Anything after // symbol in a given line is not read by compiler therefore you can add any comments like this so it will explain what the program does.There are a few other ways to add comments as well,You can find these from the site I have mentioned above.

3. The blank Lines

  These lines play no part in the program but it is to increase the readability of the program.If you want the whole code may be typed in one except the comments of course,but it is not advisable. 


Important Java is a Case sensitive language therefore your code should be identical to what I have given,I advice you not to copy paste but to type the code your self it will help you remember it better.

For more Information on this you can visit http://docs.oracle.com/javase/8/

Compiling a Java Program and Running It

To Run a Java program you need to have installed Java on your system, if you don't know how you can check my post on Setting Up Java.

Step 01:Open Command prompt

Open your command prompt, for those who do not know here is a way to do it.

1. Press the Windows key + R key you will get the run window,type cmd in the window and run it.
2. Or else you open the run window from your start menu and then type cmd in the window and
    run  it.
3.  Another way is search cmd in your search bar and you will get something called command               prompt click it to open.

Now that You have opened command prompt it should look something like this.














Step 02:Set the Path

If you haven't used Java before  you will have to set the path to the Java directories so the computer knows where to look when running Java programs.
To set the path you will have to type the bold text  in the command prompt and hit Enter,(this is assuming that you have installed java with the default setting otherwise you have to enter the address where you have installed Java)

set "PATH=%PATH%;C:\Program Files\Java\jdk1.8.0_20\bin".

Step 03:Compile a Java Program

Now you have to change the directory of the command prompt to where your .java file is, for an example let's assume the .java file is in your D drive in Folder named java ex.
First type D: in the command prompt and hit enter now you will see that instead of C in the corner of the screen you will see D.

Then type cd D:\java ex and hit Enter.
Now the Command prompt window should look like this.


Now type javac fileName.java and hit Enter.
If you get the below result it means that your file has most probably compiled.The result being nothing displayed and the next line of the command prompt being displayed(file name assumed to be HelloWorld)


If you get a text saying,

'javac' is not recognized as an internal or external command,
operable program or batch file.

This means you haven't set the path correctly,go back and check it at the top of the post.

Step 04:Run A Java Program

Once you compile the .java file a .class file is formed in the same directory,
Run the program by typing the following,

java fileName

Then you should get the result below if the program is a HelloWorld program.




That's How you Compile and Run a Java Program.











Friday, January 30, 2015

Setting Up Java On Your Computer

If you are reading this blog either you know what Java is or you know who I am, For those Who don't know what Java is let me tell you that it is language used in programming.If you want to learn Java you should first install it on your computer.Let me show you how.

Step 01:Where to find Java

You need both Java Runtime Environment (JRE) and Java Development Kit(JDK) in order for you to be able to develop a program using Java, but don't worry you only need to download one file and you get both features.

The Latest Version of Java is 1.8 or just said  8.You can get  the Java setup from here

The above download will help you get the whole Java Package.

Step 02:Installing Java

 Run the setup and click on install, and follow further instructions.
















Step 03: Check if you Have Java Installed on Your Computer

Type the java -version in your command prompt window and if you have Java installed you will see a result as follows depending on the version of Java you installed.
















That's it now you have Java and you can start programming.