An unconventional beginning
Generally we write a HelloWorld program in java first going by the convention and obsolete ways. Let us start with a simple addition program. Even if this is your first program, believe me, you will feel much more than a newbie.
DESIGN:
import java.lang.*;
class add {
public static void main(String[] args) {
int num1=12,num2=17;
System.out.println("Sum of "+num1+" and "+num2+" is :"+(num1+num2));
}
}
ANALYSIS:
- Now we analyze the code line by line.
The first line “import java.lang.*”
In this line we have imported a package. A package is a directory structure within which we have a number of classes defines by the sun java development team, placed at different levels in the directory structure. Thus importing “java.lang.*” imports all the classes in the ‘lang’ directory, the ‘*’ behaving as a wildcard. You can view the classes in your jdk installation folder, ’src’ subfolder. Try not to change anything please! As we have used the System class only in this program, we can write “import java.lang.System”, or “java.io.PrintStream” as well. That works fine for this program. The ‘import’ statement is very different from ‘#include’ in c++.
- The second line “class add”
In this line we have created a class ‘add’. Every java program has at least one class. This class implements a particular task or a part of a task and can be accessed through objects, which are instances of the class.
- The third line “public static void main(String[] args)”
This is the main function. Just like C++, the class with the main function is instantiated and called first. Thus every program has exactly one main class with a public access. The main class is ‘public’ for full external access, returns a ‘void’ to the OS and is ’static’ so that it can be invoked without creating an instance of the containing class. The ‘String[] args’ is the command line argument which is a parameter for the main method. Let us say, we want to use some argument with the runtime call, the ‘String[] args’ lets us do that (String[] args will be dealt with in further posts).
- The fourth line “int num1=12,num2=17″
In this line we have declared 2 variables num1 and num2 of integer type.
- The fifth line “System.out.println(“Sum of “+num1+” and “+num2+” is :”+(num1+num2))”
In this line we have used the System class from ‘java.lang’. The ‘java.lang’ package has around 170 classes. The System class is documented by sun as:
* The System class contains several useful class fields
* and methods. It cannot be instantiated.
*
*
Among the facilities provided by the System class
* are standard input, standard output, and error output streams;
* access to externally defined properties and environment
* variables; a means of loading files and libraries; and a utility
* method for quickly copying a portion of an array.
This code makes the class non instantiable:
/* First thing---register the natives */
private static native void registerNatives();
static {
registerNatives();
}
/** Don't let anyone instantiate this class */
The System class has an instance of the ‘PrintStream’ class which is named ‘out’. Thus System.out.println(……). The ‘PrintStream’ class has a ‘println’ method which can be overloaded(overloading will be dealt with in further posts) with a variety of parameters which are boolean, character, integer, float, double, String, long and char[]. The println method calls print() method The print() method is defined as:
* Prints a string. If the argument is null then the string
* "null" is printed. Otherwise, the string's characters are
* converted into bytes according to the platform's default character
* encoding, and these bytes are written in exactly the manner of the
* {@link #write(int)} method.
*
* @param s The String to be printed
*/
public void print(String s) {
if (s == null) {
s = "null";
}
write(s);
}
That is the end of this program. From this simple program we have come to know a variety of aspects of the Java language. If you don’t believe me, you can try asking anyone who is confident in java basics about the actual origin of the println() function we have learnt. Three out of five wont’ be able to answer, in most cases. Hope you liked the first cup!!!!
