Java - Basic Program

When we consider a Java program it can be defined as a collection of objects that communicate via invoking each others methods. Let us now briefly look into what do class, object, methods and instance variables mean.

  • Object - Objects have states and behaviors. Example: A dog has states-color, name, breed as well as behaviors -wagging, barking, eating. An object is an instance of a class.

  • Class - A class can be defined as a template/ blue print that describe the behaviors/states that object of its type support.

  • Methods - A method is basically a behavior. A class can contain many methods. It is in methods where the logics are written, data is manipulated and all the actions are executed.

  • Instance Variables - Each object has its unique set of instance variables. An object's state is created by the values assigned to these instance variables


First Java Program:

Let us look at a simple code that would print the words Simple Test Promgram.


public class SimpleProgram {

    public static void main(String []args) {
       System.out.println("Simple Test Program");
    }
}	

Lets look at how to save the file, compile and run the program. Please follow the steps given below:

  • Open notepad and add the code as above.

  • Save the file as : SimpleProgram.java.

  • Open a command prompt window and go o the directory where you saved the class. Assume its D:\.

  • Type ' javac SimpleProgram.java ' and press enter to compile your code. If there are no errors in your code the command prompt will take you to the next line.( Assumption : The path variable is set).

  • Now type ' java SimpleProgram' to run your program.

  • You will be able to see ' Simple Test Program ' printed on the window.

C : > javac SimpleProgram.java
C : > java SimpleProgram
Simple Test Program

Explain public static void main (String args[])…..

The public keyword is an access specifier, which allows the programmer to control the visibility of class members. When a class member is preceded by public, then that member may be accessed by code outside the class in which it is declared.In this case, main( ) must be declared as public, since it must be called by code outside of its class when the program is started. The keyword static allows main( ) to be called without having to instantiate a particular instance of the class. This is necessary since main( ) is called by the Java interpreter before any objects are made. The keyword void simply tells the compiler that main( ) does not return a value. As you will see, methods may also return values.
As stated, main( ) is the method called when a Java application begins. Keep in mind that Java is case-sensitive. Thus, Main is different from main. It is important to understand that the Java compiler will compile classes that do not contain a main( ) method. But the Java interpreter has no way to run these classes. So, if you had typed Main instead of main, the compiler would still compile your program. However, the Java interpreter would report an error because it would be unable to find the main( ) method.
Any information that you need to pass to a method is received by variables specified within the set of parentheses that follow the name of the method. These variables are called parameters. If there are no parameters required for a given method, you still need to include the empty parentheses. In main( ), there is only one parameter, albeit a complicated one. String args[ ] declares a parameter named args, which is an array of instances of the class String. Objects of type String store character strings. In this case, args receives any command-line arguments present when the program is executed.