Parag’s Den

Dedicated to my adored friends…..

Java Basics

Posted by paragdongre on April 16, 2009

History of Java
Java was developed by a team led by James Gosling at Sun Microsystems , a company best known by for its workstations. Java was originally called Oak and designed for use in embedded consumer electronic applications in 1991. It was redesigned for developing Internet application and renamed Java in 1995. Java Program can be embedded in HTML pages and downloaded by Web Browsers to bring live animation and interaction to Web clients.
The power of Java is not limited to web applications. Java is a general purpose programming language. It has full programming features and can be used to develop stand alone applications. Java is inherently object – oriented. Although many object- oriented languages began strictly as procedural languages. Java was designed to be object oriented from the start. Object oriented programming (OOP) is currently popular programming approach that replaces the traditional procedural programming technique.
Characteristics of Java
At the designed time of Java three main objective are taken into consideration viz. Portability (Platform Independent) , Architecture Neutral (Hardware Configuration of Machine), Security of programming codes. But apart from the these main character Java has so many another character. They are as follows.
A) Simple
B) Secure
C) Portable
D) Object-Oriented
E) Robust
F) Multithreaded
G) Architectural Neutral
H) Interpreted
I) High Performance
J) Distributed
K) Dynamic

    Simple :

No one language is Simple to learn but Java is bit easier compared to the popular Object Oriented Programming language like C++. Actually Java is partially modeled after C++ means it uses C++ programming syntax and object oriented features. But Java is great simplified and improved. For instance Pointers and Multiple Inheritance concepts are eliminated from Java instead of this simple structure interfaces are introduced. In Java specification we have so many in built packages, classes and method so that we can reuse it without rewriting the codes.
Java uses automatic memory allocation and garbage collection while in C++ we have to require to allocate memory to garbage collection.

    Java is Object Oriented

Object oriented programming models the real world. Everything in the world can be modeled as an object. A circle, A person, A window icon is an object. A Java Program is called object – oriented because programming in Java is centered on creating objects , manipulating objects and making objects working together. Java program is mostly a collection of objects talking to other objects by invoking each other’s methods. Every object is of certain type and that type is defined by a class or an interface. Most Java Program use a collection of objects of many different type.
i)Class : A template that describe the kinds of state and behavior that objects of its type support.
ii)Object: At runtime , when the Java Virtual Machine (JVM) encounters the new keyword , it will use the appropriate class to make an object which is an instance of that class. For Example
Suppose Demo is a class and is define as
Public class Demo
Int a , b; // instance variables
Public void show() // behavior (instance method)
System.out.println(“A: “+a+”B: “+b);
Statement for creating object is:
Demo d = new Demo();
Above statement is defined as with the help new keyword called to the constructor of data type of class Demo and after calling of the constructor initialize the member variable with their default value of their respective data types if not explicitly define and allocate the memory space and reference of this Object are kept into the reference variables d of type Demo . That object will have its own state , and access to all of the behaviors defined by its class.
iii)State (instance variable) : Each object (instance of a class) will have its own unique set of instance variables as defined in the class. The value assigned to an object’s instance variables make up the object’s state.
iv) Behavior(method) : When programmer creates the class he creates the methods for that class. Methods are where the class’ logic is stored. Methods are where the real work gets done. They are where algorithms get executed and data gets manipulated.
In short , An object has a properties and behaviors. Properties are described by using data and behaviors are defined by using methods. Objects are defined by using classes in Java. A class is like a template for the objects. An Object is a concrete realization of a class description.

    Java is distributed:

Distributed computing involves several computers on a network working together. Java is designed to make distributed computing easy because it handles TCP / IP protocols

    Java is interpreted: (Platform Independent)

The java programs are compiled into Machine code called bytecode. This code can be interpreted on any system that provides a Java Virtual Machine. This Bytecode is machine independent and can run on any machine that has a java interpreter. Usually a compiler translates a high level language program to machine code. The code can only run on the native machine. If you want this same program on other machine, the program has to be recompiled on native machine. For instance, if you compile C program in widows, the executable code generated by the compilers can only run on the windows platform.
With Java, you compile the source code once and the bytecode generated by Java compliers can run on any platform. The Java Program does not need to be recompiled on a target machine.

    Java is Robust:

Robust means reliable. Early checking for possible errors (mishandled exceptional conditions) during execution time with the help of object oriented exception handling construct. Java has a runtime exception handling feature to provide programming support for robustness. Java can catch and respond to an exceptional situation so that the program continues its normal execution and terminate gracefully when a runtime error occurs.
It does not support pointers which eliminate the possibility of overwriting memory and corrupting data. Java support automatic memory management by allocating memory to object if scope of object is over it automatically deallocate memory with help of Garbage Collection.

    Java is Secure

This required the elimination of many features of C and C++. Most notably there are no pointers in Java. Java programs cannot access arbitrary addresses in memory. All memory access is handled behind the scenes by the (presumably) trusted runtime environment. Furthermore Java has strong typing. Variables must be declared Casts are strictly limited to casts between types that make sense. Thus cast such as int to a long or a byte to a short can be done but not a long to a boolean or an int to a String.

Java implements a robust exception handling mechanism to deal with both expected and unexpected errors. The worst that an applet can do to a host system is bringing down the runtime environment.
Most importantly Java applets can be executed in an environment that prohibits them from introducing viruses, deleting or modifying files, or otherwise destroying data and crashing the host computer. A Java enabled web browser checks the byte codes of an applet to verify that it doesn’t do anything nasty before it will run the applet.
During runtime JVM only detect bytecode and any other are ignored that’s why there is no possibility of executing any other code. So no virus programs are executed. Hence Java code is bug free.

    Java is Architecture Neutral

The most remarkable feature of Java is that it is architecture neutral (platform independent). You can write one program that runs on any platform with JVM.

    Java is Portable

Java Program can run on any platform without having to be recompiled. In java language there is platform specific features in the Java Language Specification. For ex. In Ada language the largest integer varies on different platform. But in Java, the size of the integer is same on every platform. Having a fixed size for numbers makes the program portable. The Java environment itself is portable to new hardware and operating systems. In fact the Java compiler itself written in Java.

    Java is High Performance

Java byte codes can be compiled on the fly to code that rivals C++ in speed using a “just-in-time compiler.” Several companies are also working on native-machine-architecture compilers for Java. These will produce executable code that does not require a separate interpreter, and that is indistinguishable in speed from C++. The execution of the bytecode is never as fast as a complied language such as C++. Because Java is interpreted, the bytecode is not directly executed by the system. The bytecode is run through the interpreter.
It is certainly possible to write large programs in Java. The HotJava browser, the Java Workshop integrated development environment and the javac compiler are large programs that are written entirely in Java.

    Java is Multithreaded

Multithreading is the capability for a program to perform several tasks simultaneously within a program.
Java is inherently multi-threaded. A single Java program can have many different threads executing independently and continuously. Three Java applets on the same page can run together with each getting equal time from the CPU with very little extra effort on the part of the programmer.
Unfortunately multithreading is so tightly integrated with Java, that it makes Java rather difficult to port to architectures like Windows 3.1 or the PowerMac that don’t natively support preemptive multi-threading.
There is a cost associated with multi-threading. Multi-threading is to Java what pointer arithmetic is to C, that is, a source of devilishly hard to find bugs. Nonetheless, in simple programs it’s possible to leave multi-threading alone and normally be OK.

    Java is Dynamic

Java was designed to adapt to an evolving environment .Java Program carry with them substantial amounts of run time type information that is used to verify and resolve access to objects at run time. This makes it possible to dynamically link code in safe and expedient manner.
For Example : in the circle class you can add a new data property to indicate the color of the circle and a new method to obtain the circumference of the circle. The original client program that uses the Circle class remains the same. Also at runtime , Java loads classes as they are needed.
Java does not have an explicit link phase. Java source code is divided into .java files, roughly one per each class in the program. The compiler compiles these into .class files containing byte code. Each .java file generally produces exactly one .class file.
The compiler searches the current directory and directories specified in the CLASSPATH environment variable to find other classes explicitly referenced by name in each source code file. If the file that is compiled depends on other, non-compiled files the compiler will try to find them and compile them as well. The compiler is quite smart, and can handle circular dependencies as well as methods that are used before they’re declared. It also can determine whether a source code file has changed since the last time it was compiled.
More importantly, classes that were unknown to a program when it was compiled can still be loaded into it at runtime. For example, a web browser can load applets of differing classes that it’s never seen before without recompilation.
Furthermore, Java .class files tend to be quite small, a few kilobytes at most. It is not necessary to link in large runtime libraries to produce a (non-native) executable. Instead the necessary classes are loaded from the user’s CLASSPATH

    Java and Internet Programming (World Wide Web)

Java Program can run on from a Web Browser. Because Java is full blown programming language you can make the program responsive and interactive with the user. Java Program that run from Web page are called Applets.
Applets use a modern graphics user interface including button, text fields , text area , option buttons and so on. Applets can respond to user events such as mouse movements and keystrokes.

    Garbage Collection

There is no need to explicitly allocate or deallocate memory in Java. Memory is allocated as needed, both on the stack and the heap, and reclaimed by the garbage collector when it is no longer needed. There’s no malloc(), free(), or destructor methods.
There are constructors and these do allocate memory on the heap, but this is transparent to the programmer.
Most Java virtual machines use an inefficient, mark and sweep garbage collector.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: