Introducing Java
 
Published: 20 Jun 2007
Abstract
The basics of writing a program in Java are explained descriptively along with meanings of the keywords used in Java programs with examples to be understood by any person (technical or non-technical) who wants to learn about Java.
by Debjani Mallick
Feedback
Average Rating: 
Views (Total / Last 10 Days): 27645/ 39

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



User Comments

Title: Correction   
Name: Amit
Date: 2011-11-09 6:58:49 AM
Comment:
Bro u have to type properly.Its James Gosling not Games Gosling :)
Title: Beginners   
Name: Good Job
Date: 2010-11-30 1:29:11 AM
Comment:
It is really a good article for the bignners.
Title: It's really very nice   
Name: Alka
Date: 2009-12-11 5:29:39 AM
Comment:
Thanks for providing such an article.It's really great.....
Title: Good Article   
Name: Ranjan
Date: 2008-08-12 1:29:03 PM
Comment:
I dont know about others and I really liked this article. It was very informative.
Title: dis is hard   
Name: cook
Date: 2008-08-12 6:26:42 AM
Comment:
this is too hard and try to finish it
Title: more details should be there   
Name: Gururaj
Date: 2008-05-07 4:00:35 AM
Comment:
For any begineer, this article is not good to start
Title: Why java doesnt support multiple inheritance?   
Name: Sujata Sachdev
Date: 2008-01-16 4:30:39 AM
Comment:
Technically speaking, Java is a fool proof programming language. Every concept has been well understood before implementation.
Title: good job   
Name: Anuva
Date: 2007-06-29 3:44:46 AM
Comment:
Nice article.Very useful for bigners!
Title: Reply for(samarendra swain) why Java does not include multiple inheritance   
Name: Debjani Mallick
Date: 2007-06-25 8:12:57 AM
Comment:
Java was actually designed to be simple,familiar and object-oriented.To keep it simple and familiar, the language has been made as similar as possible to C++ but without including the unnecessary complexities of the C++ language like multiple inheritance which causes more problems and confusion than it solves.The avoidance of multiple inheritance from Java language is to avoid the "diamond" problem associated with implementation of multiple inheritance.The diamond probem is the ambiguity that arises when two classes B and C inherit from A and another class D is inherited gform both B and C.Now if a method in D calls a method defined in A,which class does it inherit via, B or C?To avoid this situation, multiple inheritance is avoided in Java.Instead we have Java implement multiple inheritance by using interfaces.
Title: Java for beginner   
Name: Arindam Parida
Date: 2007-06-25 5:20:15 AM
Comment:
It is really a good article for beginners.
Title: java does not support multiple inheritance   
Name: samarendra swain
Date: 2007-06-25 3:22:05 AM
Comment:
Thank u for article.i have a question that
why java does not support multiple inheritance?
i need the exact answers...i know that becz of complexity it doesn't support. still i wanna know the real deep answer.can u help me?
Title: Nice Article for the Beginners   
Name: Chandrakanta Kar
Date: 2007-06-25 3:03:32 AM
Comment:
It will help for the peoples working in different Technology.
Title: good   
Name: asabdjs
Date: 2007-06-23 11:54:18 PM
Comment:
nice one
Title: Abhinash   
Name: Good One
Date: 2007-06-22 1:47:19 PM
Comment:
Its really great keep up the good work!
Title: Tim   
Name: good work
Date: 2007-06-22 12:37:36 PM
Comment:
Keep up the good work
Title: Excellent   
Name: John
Date: 2007-06-20 12:26:44 PM
Comment:
The article is just fabulous.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-28 7:50:55 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search