Lesson 1: A Java Program

Java programming made easy


If this is your first time learning Java, then you would be surprised to know that Java programs are not straight forward as the Python programs are. Java is mainly an Object Oriented Programming (OOD) language. By OOD, it is meant that a program is made up of objects that interact with one another by actions described as methods. These methods are what we sometimes refer to as functions. It is said that object of the same kind (belonging to the same class) are of the same type and every object knows its methods. Also note that in Java, every object belongs to a class.
There are two types of Java programs name an applet and a regular Java program. The applet is a small program that can be run from any location via a web browser. The regular Java program is a class that contains a method called main. The main method can only be contained in one class per program and is automatically invoked during run-time. As a rule of thumb, your first program in any language is hello world and in Java, it would be saved as HelloWorld.java

public class HelloWorld
{
   public static void main(String[] args)
   {
      System.out.println("Hello World!");
   }
}
In the program above, you should notice these things
  • name of the program: Hello World
  • class name: HelloWorld
  • main method
  • argument: Hello World!
  • object: System.out
  • method: println
Remember above when we said a Java program is made up of object that interact with each other by using methods. This is exactly what is happening in the above program. The object System.out is used to send output to the screen and it has a method called println that is used to do just that.
When the line System.out.println("Hello World!") is executed, it is said that the object is invoking or calling the method to perform some action. This particular method requires additional information that can be passed as an argument "Hello World!".

You also noticed that there are other things in the HelloWorld.java program above that probably don't make sense.  One of these is the world public appearing twice in the program. The word public is what is known as an access modifier. The first public appearing before class means that the program HelloWorld is accessible from a location outside the file and the second public means that the main method is accessible from outside the class.

Share your thoughts