Writing a Java program
Java compiler does not have any editor of its own. So a Java
program can be written on a simple text editor which can be provided by any OS.
The extension name of the source file containing a Java
program must be .java.
Whenever that source file is converted into byte code with
the help of Java compiler then a new file is created having extension name
.class.
Description of the program
For writing a program first we declare a class.
Figure 1
Class - A class is an abstract data type which contains some
members. The members of a class can be either member data or member functions.
A Java function is a collection of some classes. Hence, everything must be
present within the class. Thus, the program begins with the keyword class
followed by a classname where the classname is an identifier for the class.
Public – Basically each class member (may be a member
function or member data) of a class can be either private or public. Private
and public are modifiers or access specifiers.
Listing 1
class set
{
private int x;
display ()
{
System.out.println(x);
//Here we can access the value of x as here display function is a member of class //set
}
}
class show
{
display ()
{
System.out.println(x);
//Here we cannot access the vales of x as here class show is another class
}
}
If a member is declared as private then it cannot be
accessed by the member of other class. The private members can be accessed only
by the members of the same class.
If a member is declared as public then it can be accessed by
the members of the same class as well as the members of other classes.
Members can also be declared as protected. The protected
members behave like private members for those which are not the member of the
same class, but protected members of a class can be accessed by the methods of
inherited class.
In every Java program, the main function should be publicly
declared because it must be invoked by the operating system which is not a
member of the class.
Static – The main function is declared as a static function
because it must be invoked without the help of an object i.e. without having
instantiating the class.
Void – The return type of the main function is void meaning
it returns null.
String a[ ] – Since each Java program must be executed from
the command prompt, some arguments can be passed to the main function. The
arguments are manipulated as string.
Let me give an example of a Java program to print “Hello
Friends” in one line and in the next line "Welcome to Java."
Listing 2
class example
{
public static void main(String a[ ])
{
System.out.println("Hello Friends");
System.out.print("Welcome to Java");
}
}
We can write System.out.print instead of System.out.println
in the first statement, but it will print both the statements in same line.
Println puts a line break after a line. System.out.print is a console
statement. Here, System is a predefined class for manipulating the console, out
is an object of system class and print is a method of class System.
The above program can be written in a text editor. After
writing the above lines of code in a text file, save the file with extension
.java. Let us assume that it is stored as example.java.
After saving the file we need to convert the program to a
form which can be understood by the Java Virtual Machine (JVM) so that any
computer with JVM installed can interpret and run the program. For that we need
to compile the program. Compiling a program converts the source code to byte
code which is platform independent instructions for the JVM. The Java compiler
is invoked as follows.
Listing 3
Once the program is compiled we need to interpret and run
the program which means we need to invoke the JVM byte code interpreter which
would convert the byte codes to platform dependent machine code so that your
machine can understand it and run the program. The Java interpreter is invoked
as follows.
Listing 4
At the command prompt you would see:
Listing 5
Hello Friends
Welcome to Java
Now it is a success. The above program is just a small example
in Java. Java includes many complex functionalities. Below is an example of a
simple java program for adding two complex numbers.
Listing 6
class complex
{
private int real;
private int imag;
public void get(int x,int y)
{
real = x;
imag = y;
}
public void display()
{
if(imag < 0)
{
System.out.println(real +“” +imag +“i”);
}
else
{
System.out.println(real +“+” +imag +“i”);
}
}
public void add(complex T)
{
real = real +T.real;
imag = imag +T.imag;
}
}
class show
{
public static void main(String a[ ])
{
complex C1 = new complex();
complex C2 = new complex();
C1.get(2,4);
C2.get(3,6);
C1.display();
C2.display();
C1.add(C2);
System.out.println(“The sum is::”);
C1.display();
}
}