Monday 26 October 2015

Welcome To Java


As I said earlier, Java is a platform independent programming language that can also be embedded into HTML pages.
All Java source files ends with .java extension which means that if you want to save a file written in java programming language to be run by the computer, you should save with .java. For Example: helloworld.java, welcome.java, computearea.java

This may be a little bit confusing for you now but with time you would understand everything. Every class is compiled into a separate bytecode file that has the same name as the class and ends with the .class extension.
Since we are making use of NetBeans IDE you would probably have nothing to do with files with .class extension so for now so let's keep going.


Every Java program is a set of class definitions and the contents of this class is included in a block.
Every Java program to be executed must have a main method but not all java files must have a main method. The main method is where the program starts during execution.

Soon you would know what a Java statement is but know this. Every statement in Java ends with a semicolon (;).
Also know that java source codes are very case sensitive and that reserved words or keywords used in Java cannot be used for any other reason in a Java program because it has a particular meaning to the compiler.

A line comment in Java is preceded by two slashes (//) while a block comment is enclosed between /* and */ on one or several lines.

Another thing to note is that in Java there are two types of import statements which are:
Specific Import
Wildcard Import

The specific import specifies a single class in the import statement while a wildcard import specifies all the classes in a package.

Yes all these above is sounding somehow but I say to you don't worry my dear, we would get there.

Let's begin now with codes - It's about time!
In the previous post I taught you how to create a new file write? YES
Good.
Now create a new file and name it HelloMe and select the package you created from the previous post then click finish.

You would see something like this:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nicholas;
/**
 *
 * @author Anazor
 */
public class HelloMe {
    
}

Now do you remember what I told you about block comments? YES
Do you see them now? YES
This means that all those things written within the block comments cannot be understood by the computer they are just meant for you to understand. So since this comments are not necessary to us. I will wipe them out leaving us with this:

package nicholas;
public class HelloMe {
    
}

Do you remember what I said about reserved words or keywords above? YES
Good. Now look at your NetBeans IDE you would notice all the words in colour blue? YES
Those words in colour blue are known as keywords.

Do you remember what I said about classes above? YES
Every Java program must have at least a class and this class is primarily the name of the file. You should also know that there is a particular way of writing classes and saving files in Java which is:
Each word should begin with a capital letter.
For Example If you look at our premature code above you would find out that I used HelloMe not hellome, helloMe, Hellome. Do you get the gist? You can post your answer in the comment section.

Do you see what I wrote about the main method above? YES
Good. Every Java program must have a main method for it to execute and there is a particular way of writing this main method which is:

public static void main (String [] args){

}
The use of those curly brackets is to indicate that it is a block
Therefore when I wrote 
public class HelloMe{

}
I wrote a block too.
Now integrating our main method into our class we are putting a block inside a block.
For us to execute this our Java Program called HelloMe we have to add a main method therefore, our new program is:
package nicholas;
public class HelloMe{
public static void main (String [] args){

}
}
I hope you know that my package and yours should not be the same.
Just know that the package is the name of the project for now. I will explain more as we go further.

Now these sets of instructions would definitely run if ran but it would not show anything at all. Therefore, in a way it is useless.
We need a command that can display something on the computer screen when we run. In other ways we need a command that can tell the computer SHOW THIS OR SHOW ME OR DISPLAY ME and that command is:

System.out.println();

WOW!! This command looks somehow right? YEA!! Its too techy mehn. lol
So note somethings:
First In writing this command it must start with a capital letter.
The out you see in the command signifies that the system should output i.e. send Out and the print signifies the system should display whatever that is enclosed in the bracket to the whole wide world on the computer screen.
The ln(LN in caps not IN) after print means tell the computer to move to the next line after displaying whatever that is enclosed in the bracket.
So to say hello to me here is the command I would write:

System.out.println("Hello Idoko Nicholas Chinazom!");

Now notice something else the use of semicolon (;). I talked about that right? YES I DID. This semicolons are statement terminators.

Hmmm... what has terminators (Arnold Schwarzenegger) got to do with Java? I HONESTLY DON'T KNOW. lol

Well, that's what it is called. You know in English language what a statement signifies right? YES
Good! 
Then with your knowledge of what a statement means in English bring it down to Java. It all means the same thing.

Primarily, In Java if it is not a block, it is a statement.

Therefore our final HelloMe Java Program looks like this:

package nicholas;
public class HelloMe{
public static void main (String [] args){
System.out.println("Hello Idoko Nicholas Chinazom!");
}
}

If you observe you will see that in our program there are two statements and two blocks.

To run first click on a drop down box that you see beside a hammer that has <default config> written on it and then click customize.

A dialog comes up then you click on browse on the Main Class entry.
Select the class you want to run - my own is nicholas.HelloMe - and then click on OK.

After that you see a play button in colour green - that is the run button.
Click on it and your program would run. If your code is correct it would build and run successfully else it would show you your errors.
Mine ran successfully see my console below




Here is a screenshot of my code on NetBeans



If yours ran successfully I welcome you to Java programming.
If yours did not run successfully please comment let me help you and welcome you to Java programming ok?

Peace!!

1 comment: