Introduction to
Java
BY MOHINI MISHRA
What is Java?
A simple, object oriented, distributed,
interpreted, robust, secure, architecture neutral,
portable, high performance, multithreaded,
dynamic language.
Write Once Run Anywhere (WORA).
Designed for easy Web/Internet applications.
Widespread acceptance.
How is Java different from C…
C Language:
Major difference is that C is a structure oriented language and
Java is an object oriented language and has mechanism to
define classes and objects.
Java does not support an explicit pointer type
Java does not have preprocessor, so we cant use #define,
#include and #ifdef statements.
Java does not include structures, unions and enum data types.
Java does not include keywords like goto, sizeof and typedef.
Java adds labeled break and continue statements.
Java adds many features required for object oriented
programming.
How is Java different from C++…
C++ language
Features removed in java:
Java doesn’t support pointers to avoid unauthorized
access of memory locations.
Java does not include structures, unions and enum data
types.
Java does not support operator over loading.
Preprocessor plays less important role in C++ and so
eliminated entirely in java.
Java does not perform automatic type conversions that
result in loss of precision.
Cont…
Java does not support global variables. Every method and
variable is declared within a class and forms part of that class.
Java does not allow default arguments.
Java does not support inheritance of multiple super classes by
a sub class (i.e., multiple inheritance). This is accomplished by
using ‘interface’ concept.
It is not possible to declare unsigned integers in java.
In java objects are passed by reference only. In C++ objects
may be passed by value or reference.
Cont …
New features added in Java:
Multithreading, that allows two or more pieces of the same program to
execute concurrently.
C++ has a set of library functions that use a common header file. But
java replaces it with its own set of API classes.
It adds packages and interfaces.
Java supports automatic garbage collection.
break and continue statements have been enhanced in java to
accept labels as targets.
The use of unicode characters ensures portability.
Cont …
Features that differ:
Though C++ and java supports Boolean data type, C++
takes any nonzero value as true and zero as false. True
and false in java are predefined literals that are values
for a boolean expression.
Java has replaced the destructor function with a
finalize() function.
C++ supports exception handling that is similar to java's.
However, in C++ there is no requirement that a thrown
exception be caught.
Java History
Computer language innovation and development occurs for two
fundamental reasons:
1) to adapt to changing environments and uses
2) to implement improvements in the art of programming
The development of Java was driven by both in equal measures.
Many Java features are inherited from the earlier languages:
B > C > C++ > Java
In 1990, Sun Microsystems started a project called Green.
Objective: to develop software for consumer electronics.
Project was assigned to James Gosling, a veteran of classic network
software design. Others included Patrick Naughton, ChrisWarth, Ed
Frank, and Mike Sheridan.
The team started writing programs in C++ for embedding into
– toasters
– washing machines
– VCR’s
Aim was to make these appliances more “intelligent”
C++ is powerful, but also dangerous. The power and popularity of C
derived from the extensive use of pointers.
However, any incorrect use of pointers can cause memory leaks,
leading the program to crash.
In a complex program, such memory leaks are often hard to detect.
Robustness is essential. Users have come to expect that Windows may
crash or that a program running under Windows may crash. (“This
program has performed an illegal operation and will be shut down”)
However, users do not expect toasters to crash, or washing machines to
crash.
A design for consumer electronics has to be robust.
Replacing pointers by references, and automating memory
management was the proposed solution.
Hence, the team built a new programming language called Oak,
which avoided potentially dangerous constructs in C++, such as
pointers, pointer arithmetic, operator overloading etc.
Introduced automatic memory management, freeing the programmer
to concentrate on other things.
Architecture neutrality (Platform independence)
Many different CPU’s are used as controllers. Hardware chips are evolving
rapidly. As better chips become available, older chips become obsolete
and their production is stopped. Manufacturers of toasters and washing
machines would like to use the chips available off the shelf, and would not
like to reinvest in compiler development every two-three years.
So, the software and programming language had to be architecture
neutral.
It was soon realized that these design goals of consumer electronics perfectly
suited an ideal programming language for the Internet and WWW, which should
be:
object-oriented (& support GUI)
robust
architecture neutral
Internet programming presented a BIG business opportunity. Much bigger than
programming for consumer electronics.
Java was “re-targeted” for the Internet
The team was expanded to include Bill Joy (developer of Unix), Arthur van Hoff,
Jonathan Payne, Frank Yellin, Tim Lindholm etc.
In 1994, an early web browser called WebRunner was written in Oak.
WebRunner was later renamed HotJava.
In 1995, Oak was renamed Java.
A common story is that the name Java relates to the place from where the
development team got its coffee. The name Java survived the trade mark
search.
JDK Editions
Java Standard Edition (J2SE)
J2SE can be used to develop client-side standalone
applications or applets.
Java Enterprise Edition (J2EE)
J2EE can be used to develop server-side applications such as
Java servlets and Java ServerPages.
Java Micro Edition (J2ME).
J2ME can be used to develop applications for mobile devices
such as cell phones.
Compiled Languages
Interpreted Languages
Java Approach
How Java Works
Java's platform independence is achieved by the
use of the Java Virtual Machine
A Java program consists of one or more files with a
.java extension
these are plain old text files
When a Java program is compiled the .java files are
fed to a compiler which produces a .class file for
each .java file
The .class file contains Java bytecode.
Bytecode is like machine language, but it is
intended for the Java Virtual Machine not a specific
chip such as a Pentium or PowerPC chip
To run a Java program the bytecode in a .class file is
fed to an interpreter which converts the byte code
to machine code for a specific chip (IA-32,
PowerPC)
Some people refer to the interpreter as "The Java
Virtual Machine" (JVM)
The interpreter is platform specific because it takes
the platform independent bytecode and produces
machine language instructions for a particular chip
So a Java program could be run an any type of
computer that has a JVM written for it.
PC, Mac, Unix, Linux, BeaOS, Sparc
execution model of Java
source bytecode
(text) compiler (aka. class file)
dynamic
loading
verifier
virtual machine
JIT compiled
bytecode code
compiler
interpreter
CPU
Write Once, Run Anywhere
Simple Program
Run java Program
source code
(Hello.java)
compile
byte code
(Hello.class)
execute
output
Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Java Is Interpreted
Java Is Robust
Java Is Secure
Java Is Architecture-Neutral
Java Is Portable
Java's Performance
Java Is Multithreaded
Java Is Dynamic
Characteristics of Java
Java Is Simple Java is partially modeled on C++, but
greatly simplified and improved. Some
Java Is Object-Oriented people refer to Java as "C++--" because it
Java Is Distributed is like C++ but with more functionality and
fewer negative aspects.
Java Is Interpreted
Java Is Robust
Java Is Secure
Java Is Architecture-
Neutral
Java Is Portable
Java's Performance
Java Is Multithreaded
Java Is Dynamic
Characteristics of Java
Java Is Simple Java is inherently object-oriented.
Although many object-oriented
Java Is Object-Oriented languages began strictly as procedural
Java Is Distributed languages, Java was designed from
the start to be object-oriented. Object-
Java Is Interpreted oriented programming (OOP) is a
Java Is Robust popular programming approach that is
replacing traditional procedural
Java Is Secure programming techniques.
Java Is Architecture-
One of the central issues in software
Neutral development is how to reuse code.
Java Is Portable Object-oriented programming provides
great flexibility, modularity, clarity, and
Java's Performance reusability through encapsulation,
Java Is Multithreaded inheritance, and polymorphism.
Java Is Dynamic
Characteristics of Java
Java Is Simple Distributed computing involves several
computers working together on a
Java Is Object-Oriented network. Java is designed to make
Java Is Distributed distributed computing easy. Since
networking capability is inherently
Java Is Interpreted integrated into Java, writing network
Java Is Robust programs is like sending and receiving
data to and from a file.
Java Is Secure
Java Is Architecture-
Neutral
Java Is Portable
Java's Performance
Java Is Multithreaded
Java Is Dynamic
Characteristics of Java
Java Is Simple You need an interpreter to run Java
programs. The programs are compiled
Java Is Object-Oriented into the Java Virtual Machine code
Java Is Distributed called bytecode. The bytecode is
machine-independent and can run on
Java Is Interpreted any machine that has a Java
Java Is Robust interpreter, which is part of the Java
Virtual Machine (JVM).
Java Is Secure
Java Is Architecture-
Neutral
Java Is Portable
Java's Performance
Java Is Multithreaded
Java Is Dynamic
Characteristics of Java
Java Is Simple Java compilers can detect many
problems that would first show up at
Java Is Object-Oriented execution time in other languages.
Java Is Distributed
Java has eliminated certain types of
Java Is Interpreted error-prone programming constructs
Java Is Robust found in other languages.
Java Is Secure Java has a runtime exception-handling
feature to provide programming
Java Is Architecture-
support for robustness.
Neutral
Java Is Portable
Java's Performance
Java Is Multithreaded
Java Is Dynamic
Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Java Is Interpreted
Java implements several security
Java Is Robust
mechanisms to protect your system
Java Is Secure against harm caused by stray
programs.
Java Is Architecture-
Neutral
Java Is Portable
Java's Performance
Java Is Multithreaded
Java Is Dynamic
Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Java Is Interpreted
Java Is Robust
Java Is Secure
Java Is Architecture- Write once, run anywhere
Neutral
Java Is Portable With a Java Virtual Machine (JVM), you
can write one program that will run on
Java's Performance any platform.
Java Is Multithreaded
Java Is Dynamic
Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Java Is Interpreted
Java Is Robust
Java Is Secure
Java Is Architecture-
Neutral
Because Java is architecture neutral,
Java Is Portable Java programs are portable. They can
Java's Performance be run on any platform without being
recompiled.
Java Is Multithreaded
Java Is Dynamic
Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Java Is Interpreted
Java Is Robust
Java Is Secure
Java Is Architecture-
Neutral
Java’s performance Because Java is
Java Is Portable architecture neutral, Java programs
Java's Performance are portable. They can be run on any
platform without being recompiled.
Java Is Multithreaded
Java Is Dynamic
Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Java Is Interpreted
Java Is Robust
Java Is Secure
Java Is Architecture-
Neutral
Java Is Portable
Java's Performance Multithread programming is smoothly
integrated in Java, whereas in other
Java Is Multithreaded languages you have to call procedures
Java Is Dynamic specific to the operating system to enable
multithreading.
Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Java Is Interpreted
Java Is Robust
Java Is Secure
Java Is Architecture-
Neutral
Java Is Portable
Java was designed to adapt to an
Java's Performance evolving environment. New code can be
loaded on the fly without recompilation.
Java Is Multithreaded
There is no need for developers to create,
Java Is Dynamic and for users to install, major new software
versions. New features can be
incorporated transparently as needed.
So really, why learn about
programming?
Programmers make lots of money.
Programming really is fun.
Programming is very intellectually rewarding.
Programming makes you feel superior to
other people.
Programming gives you complete control
over an innocent, vulnerable machine, which
will do your evil bidding with a loyalty not
even your pet dog can rival.