AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=1295&pId=-1
Introducing Java
page
by Debjani Mallick
Feedback
Average Rating: 
Views (Total / Last 10 Days): 27573/ 71

Overview

Java is a purely object-oriented programming language developed by Sun Microsystems in 1991. Initially it was named "Oak" by Games Gosling and his team. In 1995, it was renamed as Java. Being an object oriented language, it has the features of object oriented programming language embedded within it such as the following.

Encapsulation -It is the process of combining the data and codes into a single unit called class.

Inheritance -It is the process of defining a new class from an already existing class. Here the existing class is called the baseclass and the new class is called the derived class. The derived class possesses all the functionalities of the baseclass as well as some new functionalities of its own.

Polymorphism -It is the property by virtue of which the same object behaves differently under different circumstances.

Primarily the motivation of developing Java was to create a language through which writing programs should be possible so that they can be executed in any kind of CPU and with the evolution of Internet, Java became popular for writing Internet based applications.

Features of Java

Java is a platform independent programming language using which one can write a program that can run under any kind of OS.

It is also architectural neutral because the Java compiler does not generate object code which is machine dependent; rather the source file of a Java program is converted to byte code. To run this byte code we need an interpreter which is called Java Virtual Machine (JVM). Once the JVM is implemented then any Java program can be executed irrespective of the hardware architecture of the computer.

Later on in Java 2, a run time compiler has been added which generates executable code from the byte code on a piece by piece basis. That run time compiler is called Just In Time (JIT).

Differences between Java, C, C++

Java

C

Java does not support pointers, include statement like "goto" and "size of" statements.

C supports pointers, including statements like "goto" and "size of."

Java does not contain data types like structure and union.

C contains data types like structure and union.

Java does not use pre-processor directive like # include and # define.

C uses pre-processor directive like # include and # define.

 

Java

C++

Java does not support operator overloading.

C++ supports operator overloading.

Java does not support multiple inheritance.

C++ supports multiple inheritance.

Java does not support global variables.

C++ supports global variables.

How to write a program in Java

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

javac example.java

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

java example

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();
}
}
Summary

By now I think you must have gained a brief knowledge about Java. In my next article I will discuss Java in more detail.

By Debjani Mallick

MindFireSolutions.com


Product Spotlight
Product Spotlight 

©Copyright 1998-2021 ASPAlliance.com  |  Page Processed at 2021-03-09 5:17:37 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search