Monday, 4 December 2017

Java For Beginners Lesson 3: Structure of a Java Program

This lesson follows from the Lesson 2 of the Java Programming Tutorial for Beginners. So if you have not gone through lesson 1 and 2, you need to take them before you continue on Lesson 3.

In Lesson 2, you wrote your first Java program which is So, let us examine this program more closely to help you get a better knowledge of how a java program is arranged.

Notice that Netbeans gives different colors to various part of the program. This is called Syntax Coloration.
You also need to know about the word, Compiler. In simple terms, it is an application used to run computer programs. For instance Netbeans.

Comments in Java
Comments are text added to a program to provide more information about the program. Comments have no effect on the running of the program and are ignored by the compiler(know what a compiler is, right? An application used for running computer programs).

Comments can be single-line or multi-line
Single line comments begin with // and only takes one line
Multiline comments begin with /* and end with */ and can span more than one line
Can you identify the comments in the program above?

The line: package myfirstprogram
This line simply means that a collection of similar programs are placed inside the folder myfirstprogram. You will not have to write this. The system automatically writes it when you create your first program in NetBeans

The line: public class myFirstProgram
This actually the beginning of the program.
The word public is known as a keyword and for now just know that each program begins this way

The line: public static void main(...
This is the beginning of the function called main. This function main, has to be in all java programs.

The word Static means that the function main(or any other function or variable after the word static) is shared between all instances of the class.(don't worry about this for now, you'll get a clearer understanding at a later lesson)

System.out.println("Welcome to Java for Beginners");
This is a function that prints out a text to the output. This is an inbuilt function in java just as there are many others. Inbuilt functions can be used to perform different tasks. With time you will learn about more inbuilt functions.

The Semicolon (;)
Also note that the line of code we wrote ends in semicolon. All lines of code written in Java is terminated in semicolon.

So we stop here for now because we need to keep it simple and easygoing. Try to do the exercise below.

  1. Remove the semicolon that ends the program and try to run the program again. What is your observation. Write in the comment box below.
  2. Count how many curly braces { in the program. Give your answer in the comment box.
Now get ready for Lesson 3: Your Second Java Program