My First Java Program – Java for Zombies

After the first Introduction to java for Zombies post in this  issue we will see what looks like a Java program and how we can write our first Java program.

All examples and exercises in this course have been conducted using the NetBeans 8.0.2 development environment on Windows 10. To follow the course you must have it installed. It is a totally free environment and you can download it from  Netbeans.org

Java language is a pure object – oriented and does not allow the possibility to program using any other technique than this.

For this reason: A Java program will consist of one or several source files and each will be defined one or more classes.

A Java source file will look similar to this:

For a program to run it must contain a class that has a method called main with the following statement:

public static void main (String args [])

The concept of class is the basis for object – oriented programming and studied thoroughly later in the series, now do not worry about it if you’re not clear what or why you have to use these method. It is not the time to explain now, You just have to know the structure of a program and start writing basic programs.
As an example we will write the typical Hello World program.
The program simply will display the message

“Hello World !!!”

and will terminate.
As this is our first program we will explain the steps to write and run:

 

WRITE, COMPILE, AND RUN THE HELLOWORLD JAVA PROGRAM USING NETBEANS IDE ON Windows 10

  1. Run NetBeans

Launch NetBeans from Desktop

Just Double- click the NetBeans IDE icon on the desktop to launch the IDE. The version of NetBeans we are using is 8.0.2 on Windows 10

 

 

 

The NetBeans Homepage will appear like this

NetBeans Home Page

 

  1. Create a New NetBeans project

Select File -> New Project
Alternatively You can just click the new file Icon

create a new java project

 

The New Project dialog box appears
Select Java in Categories section and Java Application under Projects section.
Click the Next Button.

select catagory

 

Section Name and Location , in the field Project Name , write HelloWorld .
In the field Create Main Class , write helloworld.HelloWorld or else It will be renamed it self as soon as ypu specify the Project Name

Leave the option Set as Main Project selected.

Click on the button Finish .

Create Class

 

Note that a HelloWorld app is created in the Projects tab.
Enter the file HelloWorld.java
and enter the statement below in the main class which we named as HelloWorld
System.out.println ( “Hello World!!!!”);

as shown in the gif:

enter the program in main classs

Enter a syntax error in the program, for example, writes voide rather than void, and note that NetBeans immediately indicates the error as shown in the figure following

check for syntax errors

It seems that the reserved word void is misspelled as voide
Correct the error

  1. Run The Program

To run the program click on the green play button the bar as shown in the gif below

run the program

You can also run the program by clicking the right mouse button on HelloWorld and select Run .

alternative run

And finally we see the result of our program. NetBeans tells us that HelloWorld.java compiles and runs correctly. The program displays the message Hello World!!!!
Look in the gifs above Hello World!!!! Being displayed in green and below that is shown the execution time.

Although the program is very simple, it contains many of the concepts of object-oriented programming in Java.

The first line of the program:

class HelloWorld {
declares a class called HelloWorld.
All that is between the open brace ({) and the closing brace (}) belongs to the class HelloWorld.
In this program this is the only class that contains the main method.

All independent program written in Java starts to run from the main method ().
The main method is declared:

  • Public:indicates that the method is public and therefore can be called from other classes. Any main () must be public to be run from the Java interpreter.
  • Static: indicates that it is not necessary to create any objects of the class to use the method. It also indicates that the method is the same for all instances that may be created in the class.
  • Void:indicates that the function main returns no value.

The method main should always accept, as a parameter, a vector of strings that contain the possible arguments that are passed to the program on the command line, although as in our case, not used.

Do not worry if there are things you do not understand. All these concepts are studied in-depth along the Java for zombies series of posts

The instruction that actually does the work is:

System.out.println ( “Hello World!!!!”);

To display information on the screen the System class, which contains an attribute out which in turn contains the println method.

println displays the message and makes a line break placing the cursor at the beginning of the next line.

There is another method (print) for showing the message but does not jump line.

Some things to keep in mind when writing code:

  • Java is case-sensitive.
  • Each line of code must end; exceptions to be discussed later.
  • An instruction can occupy more than one line.

So this was the second Post in Java for Zombies series
I have tried to keep is quite simple and elaboratiive so that even a zombie 😀 can understand
comment below if you have any difficulty executing the steps above

and keep checking for the upcoming posts in the series or simply just follow on Facebook or twitter

Have a great Day!

Leave a reply