You are on page 1of 47

WELCOME TO WORLD OF

JAVA
History of Java
With the invention of internet and world
wide web there is need of a language that
should be platform independent.
On internet we don't know in advance at what
platform our code or program /application
will be executed as all over the world there
are heterogeneous systems.
JAVA was developed by Sun
Microsystems in 1991.earlier it
was called “Oak” but was
renamed Java in 1995

JAVA was not made for Internet


programming. Java was developed for
Microcontroller for platform neutral
execution. so there is no need to rewrite the
code each time for every Microcontroller.
C and C++ compiler are difficult to
develop are expensive and time
consuming process so an easier
and more cost efficient solution
was needed.
James Gosling and his team start working on
a portable ,platform independent language
that could be used to produce code that
would run on variety of CPU under different
environment.
This effort leads to creation of JAVA
If a language is portable does not
means it is Platform independent
In portable language we can compile the
code by using different compiler on different
operating system.
e.g. in C source code
 On DOS we can compile the source code by

Turbo Borland Compiler


 On Window we can compile using VC++

Compiler
 For UNIX we can use CC/GCC compiler
In case of platform independent
compilation(NO DEPENDENCY ON
OS) user don't know at what
platform his code is going to
compile
JAVA VIRTUAL MACHINE is the
solution for platform independent
compilation
WHAT IS JVM?
Specification designed by SUN Microsystems
1. Instruction set

2. File format

3. Class loading and execution Mechanism etc.

JVM is specification of JRE


JRE is implementation of JVM
JRE can be called JVM but JVM cannot be called JRE.
Car is called a Vehicle but Vehicle is not called Car
What is JRE
A software vendor who wants to develop JRE
needs JVM
Main component of JRE
1 Byte code verifier
2.Class Loader
3.Just in time Compiler
4.Class Library
5.Security Policies
6.Memory Management.
What is JDK(java development kit)
 It is the package of JRE and tools(javac ,javap ,jar
etc.)
(jar is compression and decompression format of sun micro system )
1. Application programmer needs JDK

2. End user needs JRE

3. A software vendor who wants to develop JRE


needs JVM(specification and license from
SunMicrosystem now owned by Oracle).
4. Every vendor can develop their own compiler of
java with license from sun micro system
according to their need.
BYTE CODE
The key to solve the security and portability
problem is that the output of Java compiler is not
executable code rather than it is BYTECODE.
Byte Code is a highly optimized set of
instructions to be executed by JRE(some time
called JVM)
JVM was designed as an interpreter for byte code.
JVM may be different from platform to platform
but all understand the same byte code.
It is an object oriented programming language

 In real world we think in the form of object. We give


preference to object than on work that object perform.
e.g: Teacher, Carpenter
JAVA designer want to develop a language that should
solve the problems that have solution similar real world
problems.
If we want to study we looks for teacher.
If we want to make a table, chair or any furniture we
looks for carpenter
OBJECT
 Object is an Instance of class
 At particular moment how that class is

behaving is know as object


 Suppose Light is class

Either it is on ,off,dim,bright
Light is on
Light is off
Light is bright
Light is Dim
Here Light is class
lt is object
the name of the type/class is Light,
the name of this particular Light object is lt,
and the requests that you can make of a Light object
are to turn it on,
turn it off,
make it brighter,
or make it dimmer.
You
create a Light object by defining a “reference” (lt) for that
object and calling new to request a new object of that type.
Object orientation
 it is the way of thinking in terms of object on
the basis of this thinking a programming
model created by sun micro system in known
as object oriented programming structure
 SUN MicroSystem is a microelectronics

company designing chips for microcontroller


used in washing machines ,digital set top
box,Remote control etc.
 What is difference between microcontroller

and microprocessor?
Why they need platform independent
language?
Java's main features are
 1. Platform independent.
 2. Security
If any language is platform
dependent then
 For each platform we have to write a new
code.
 If a company is making 100 microcontroller

per day then 100 times we have to write the


code. because language depends on the
properties of hardware(instruction set,
instruction format, type of register)
Main Feature of Java:
1. Simple
2. Secure(The JVM performs verification on bytecode before running it to prevent the program from
performing unsafe operations such as branching to incorrect locations)

3. Object-Oriented
4. Robust(Java is robust because it is highly supported language, meaning that unlike C
you cannot crash your computer with a bad program)

5. Multithreaded
6. Architectural Neutral
(binary code format" that's independent of hardware
architectures, operating system interfaces, and window systems)
7. Interpreted.
8. High-performance
(Just in time compilation)
9. Distributed (development of application to run over network always
need distributed languages)
10. Dynamic (During the execution of a program, Java can dynamically
load class libraries )
Limitation of Java
 It is slow in comparison of C/C++.because it is
both compiled and interpreted language.
 Applet cannot access local data. Only download
the data from site where applet is located.
 No use of pointer so we cannot do flexible data
structure design.
 Need different JVM for different platform.
 It is strictly types language (every variable have
type, every expression has type) so there is no
flexibility in programming.
Difference between C ++ and Java
JAVA C++
1 Java is completely object oriented C++ is extension of C with object oriented
language behavior
2 Java does not support multiple C++ support multiple inheritance
inheritances. But it can be achieved
using interface if needed.
3 Java does not support global Global variable can be declared in C++
variable
4 Java does not support pointer. C++ support pointers.
5 In Java there is no destructor. In C++ there is use of destructor
6 Java does not provide header file C++ has header file.
7 Java code run on Virtual machine C++ code runs on actual machine
(JVM)
8 In java automatic memory Memory management is done manually by
management and garbage collection new/delete.
9 Write once run any where Write once compile anywhere
Types of Inheritance in Java

 1 Single Inheritance
 2 Multiple Inheritance
 3 Multilevel Inheritance
 4 Hierarchical Inheritance
 5 Hybrid Inheritance
Single Inheritance

 single inheritance is simply one subclass


extending one superclass. It is the simplest of
all. 
Multiple Inheritance

 Multiple Inheritance is one class extending


multiple classes. As discussed before, Java
does not support multiple Inheritance.
However, with the help of interfaces, we can
visualize multiple Inheritance at work.
Multilevel Inheritance

 A multilevel inheritance is about a superclass


extending its features to a subclass, which in
turns act as another superclass to a new
subclass. Multilevel inheritance is
implemented in a hierarchy. This means, once
a subclass, will be a future superclass.
Hierarchical Inheritance

 In hierarchical inheritance, one class acts as a


parent class for multiple subclasses.
Hybrid Inheritance

 Hybrid inheritance is formed as a result of the


combination of both single and multiple
Inheritance. As multiple Inheritance is not
viable with classes, we achieve hybrid
inheritance through interfaces.
Simple Example:
Class MyExample
{
public Static Void main(String arr[])
{
System.out.println(“this is my example”);
}
}
 The word Class means that a new class is defined.
 MyExample is an identifier(called class name)
 All members of class should be declared inside curly
braces {…………….}
 Execution always start from main method.
 The keyword public mean that member may be
accessed by code outside the class in which it is
declared.
 main() must be declared as public since it must be
called by the code outside of its class when the
program is started.
 Keyword static allow main() to be called without
having to instantiate a particular instance of class.
since main() is called by JVM before any object are
made.
 Keyword void tells the compiler that main()
does not return a value.
 Object of type String store character string in

this case arr is an command line argument


presents when program is executed.
 System is a predefined class that provides

access to the system and out is output stream


that is connected to console.
Execution Model of C and C++

1.Application file is read and format is checked.


2.If format is valid .its instruction is loaded.
3.Address of first Inst. Is loaded in PC
4.Process asked to start execution.
5.Address of Instruction fetched from PC one by one.
6.Instruction is fetched from memory and is executed.
Execution Model of Java Class

2.Class file is read and its format is checked.


3,4.If format is valid, class loader is asked to load it.
5.Just in time compiler(JITC) is asked to execute the class.
6.Instruction of class is read one by one.
7.For each instruction security policy is checked and instruction is
executed only if it satisfy security policy.
IDE can used to edit and run
 . 
 Eclipse
Program to find the area of Rectangle with dimension passed through arguments
Class Rectangle Class RectangleTest
{
{
int l,b //attribute
public void display() public static void main(String
{ arr[])
System.out.println(“Length=”+l);
{
System.out.println(“Breadth=”+b);
}
Rectangle r = new Rectangle();
public int area() r.setDimension(20,30)
{ {
Return l*b;
System.out.println(“dimension of
}
Public void setDimension(int x,int y) rectangle are”);
{ r.display();
I=x;
System.out.println(“area of
B=y;
} rectangle r is =”+r.area());
} }}
Syntax for creating object of a class:

 Classname refrencevariable = new classname();


 Reference variable is a implicit pointer which
is used to contain the reference of objects.
 In java reference variable are dynamically

created. Hence they don’t have name and are


referenced through their reference variables.
 In C++: there is static allocation of objects
 r
 Rectangle r ======== r
l
b
Thank YOU

You might also like