Chapter 1 Introduction
Chapter 1 Introduction
Chapter 1 Introduction
History of Java
In 1990, Sun Micro Systems Inc. (US) was conceived a project to develop software for consumer
electronic devices that could be controlled by a remote. This project was called Stealth Project but
later its name was changed to Green Project.
In January 1991, Project Manager James Gosling and his team members met to discuss about this
project.
Gosling thought C and C++ would be used to develop the project. But the problem he faced with
them is that they were system dependent languages. The trouble with C and C++ (and most other
languages) is that they are designed to be compiled for a specific target and could not be used on
various processors, which the electronic devices might use.
James Gosling with his team started developing a new language, which was completely system
independent.
This language was initially called OAK. Since this name was registered by some other company,
later it was changed to Java.
James Gosling and his team members were consuming a lot of coffee while developing this
language. Good quality of coffee was supplied from a place called “Java Island’. Hence they fixed
the name of the language as Java.
The symbol for Java language is cup and saucer.
Sun formally announced Java at Sun World conference in 1995. On January 23rd 1996, JDK1.0
version was released.
When you program for the Java platform, you write source code in .java files and then compile
them. The compiler checks your code against the language's syntax rules, then writes out bytecode
in .class files. Bytecode is a set of instructions targeted to run on a Java virtual machine (JVM).
In adding this level of abstraction, the Java compiler differs from other language compilers, which
write out instructions suitable for the CPU chipset the program will run on.
At runtime, the JVM reads and interprets .class files and executes the program's instructions on
the native hardware platform for which the JVM was written. The JVM interprets the bytecode
just as a CPU would interpret assembly-language instructions. The difference is that the JVM is a
piece of software written specifically for a particular platform.
1
The characteristics of Java
Simple
Java is partially modeled on C++, but greatly simplified and improved. For instance,
pointers and multiple inheritances often make programming complicated.
Java replaces the multiple inheritances in C++ with a simple language construct called
an interface, and eliminates pointers.
Java uses automatic memory allocation and garbage collection, whereas C++ requires the
programmer to allocate memory and collect garbage.
Secure
2
Portable
Because Java is architecture neutral, Java programs are portable. They can be run on any
platform without being recompiled. Moreover, there are no platform-specific features in
the Java language.
In Java, the range of the integer is the same on every platform, as is the behavior of
arithmetic. The fixed range of the numbers makes the program portable. The Java
environment is portable to new hardware and operating systems. In fact, the Java compiler
itself is written in Java.
Java programs can execute in any environment for which there is a Java run-time
system.(JVM)
Java programs can be run on any platform (Linux, Window, Mac)
Java programs can be transferred over world wide web (e.g. applets)
Object-oriented
Robust
o Java encourages error-free programming by being strictly typed and performing run-time
checks.
o Robust means reliable. No programming language can ensure complete reliability. Java
puts a lot of emphasis on early checking for possible errors, because Java compilers can
detect many problems that would first show up at execution time in other languages. Java
has eliminated certain types of error- prone programming constructs found in other
languages. It does not support pointers, for example, thereby eliminating the possibility of
overwriting memory and corrupting data.
o Java has a runtime exception-handling feature to provide programming support for
robustness. Java forces the programmer to write the code to deal with exceptions. Java can
catch and respond to an exceptional situation so that the program can continue its normal
execution and terminate gracefully when a runtime error occurs.
3
Multithreaded
Architecture-neutral
Interpreted
The programs are compiled into the Java tfirtual Machine code called bytecode. The
bytecode is machine-independent and can run on any machine that has a Java interpreter,
which is part of the Java tfirtual Machine (JtfM).
With Java, you compile the source code once, and the bytecode generated by a Java
compiler can run on any platform with a Java interpreter. The Java interpreter translates
the bytecode into the machine language of the target machine.
Java supports cross-platform code through the use of Java byte code.
Byte code can be interpreted on any platform by JVM.
High performance
4
Distributed
Dynamic
o Java was designed to adapt to an evolving environment. New class can be loaded on the
fly without recompilation. There is no need for developers to create, and for users to install,
major new software versions.
Abstraction
An essential element of object-oriented programming is abstraction. Abstraction is a powerful
methodology to manage complex systems. Abstraction is managed by well-defined objects and
their hierarchical classification. For example a car itself is a well-defined object, which is
composed of several other smaller objects like a gearing system, steering mechanism, engine,
which are again have their own subsystems. But for humans car is a one single object, which can
be managed by the help of its subsystems, even if their inner details are unknown.
Java is an object oriented language because it provides the features to implement an object oriented
model. These features include encapsulation, inheritance and polymorphism.
OOP is about developing an application around its data, i.e. objects which provides the access to
their properties and the possible operations in their own way.
Principles of OOP
1) Encapsulation
Encapsulation is to hide the implementation details from users. If a data member is private it means it can
only be accessed within the same class. No outside class can access private data member (variable) of other
class. Encapsulation is:
5
2) Inheritance
The mechanism by which an object acquires the some/all properties of another object.
It supports the concept of hierarchical classification.
3) Polymorphism
Polymorphism (from Greek, meaning “many forms”) is a feature that allows one interface
to be used for a general class of actions.
Polymorphism means to process objects differently based on their data type.
In other words it means one method with multiple implementations, for a certain class of
action. And which implementation to be used is decided at runtime depending upon the
situation (i.e., data type of the object)
This can be implemented by designing a generic interface, which provides generic methods
for a certain class of action and there can be multiple classes, which provides the
implementation of these generic methods.
Overloading means two methods having same method name but take different input
parameters. This called static because, which method to be invoked will be decided at
the time of compilation.
Overriding means a derived class is implementing a method of its super class.
Java editors
To write Java programs, a text editor will be needed.
Notepad,
net beans,
eclipse, and
IntelliJ are the most frequently used and popular java code editors.
6
Naming Conventions
Naming conventions specify the rules to be followed by a Java programmer while
writing the names of packages, classes, methods etc.
Package names are written in small letters.
e.g.: java.io, java.lang, java.awt etc
Each word of class name and interface name starts with a capital
e.g.: Sample, AddTwoNumbers
Method names start with small letters then each word start with a
capital
e.g.: sum (), sumTwoNumbers (), minValue ()
Variable names also follow the same above method rule
e.g.: sum, count, totalCount
Constants should be written using all capital letters
e.g.: PI, COUNT
Keywords are reserved words and are written in small letters.
e.g.: int, short, float, public, void