Vous êtes sur la page 1sur 32

Programming with JAVA

Introduction of java
Java Virtual Machine
Java system overview /Java compile-time Environment
Java Buzzwords
Different between C++ and JAVA

Introduction of java
Java was developed by Jems Goslin at
Sun Microsystems in the early 1990s.
The world wide web appeared in 1993, the
language has been enhanced to facilitate
programming on web.
Since then it become one of the most
popular language for web.
This language was initially called Oak but
renamed as java in 1995.

Introduction of java
The primary motivation was the need of
platform-independent language that could be
used to create software to be embedded in
various customer electronic devices, such as
microwave ovens and remote controls.
Java never provides any tool to handle
operations related to memory.
The memory addressing is implemented and
controlled by java virtual machine (JVM).
Memory addressing mean where the variables
are created, objects are stored, and methods
are stored in memory.

Introduction of java
Java is derived from c++, because most of the
features are taken from c++.
Sun released the first JDK 1.0 in 1995.
It promised a platform independent language to
the programmer.
it was write Once Run anywhere.
Sun launched different versions for different
purposes, e.g. J2EE is enterprise edition, use for
server side programming, J2ME is micro edition
used for mobile applications, and J2SE is for
standard edition.

Introduction of wrapper class makes java a
complete OOP language.
When a java source file is compiled, a class file
is generated, which is a collection of Byte codes.
Byte codes are portable. These are later
changed to native code by JVM.
Bytes code are least affected by virus as
compared by executable files. If the Byte code is
affected, these are recognized by the JVM and
not allowed to be executed.
Java has built-in support for exception handling.
It has built-in support for multithreading
application development.
There is a built-in support for networking.

Java Virtual Machine
JVM system is an interpreter.
Java virtual machine is a software system that translates
and execute java byte code.
An instruction set and the meaning of those instructions the
byte codes
A binary format the class file format
An algorithm to verify the class file



Method
Area
Heap Java
Stacks
PC
Registers
Native
method
Stacks
Execution Engine
Class loader
subsystem
Native method interface
Native
method
libraries
Class files
Runtime data areas
JVM Architecture

1. Class loader Sub-System performs its task in a
sequential way:
It loads a class file .
It checks the correctness of the class file. If any one has
manipulated the class file, then the class file can not be
executed.
It allocate memory for static variables.
It sets the default value of all static variable.
It transforms symbolic reference into direct reference.
2. Method Area
It is a logical memory component of JVM.
This logical section of memory holds the information about
classes and interfaces.
Static variables are treated as class variables, because
they take memory from method area.


3. Heap
When object or array is created, memory is allocated to
them from heap.
JVM through the use of new operator allocates memory
from the heap for an object.
The JVM has a daemon thread known as Garbage
Collector whose task is to free those objects from heap
whose reference is not alive in stack.
4. JAVA Stack
Method codes are stored inside method area.
For execution, a method needs memory because of local
variables and the arguments it has taken.
This purpose is fulfilled by java stack.



5. Program counter register
It keeps track of the sequence of the program.
Pc register holds the address of the instructions to be
executed next.
6. Native method stack
When a java application invokes a native method, that
application does not only use java stack but also use the
native method stack for the execution of native methods.
The libraries required for the execution of native
methods are available to the JVM through Java Native
Interface.
7. Execution Engine
Generate and executes the java byte code.
It contains an interpreter and Just In Time compiler.


8. Java Native interface

JNI is used when the programmer has already
developed the code in c/ c++ and wishes to make it
accessible to java code.
JNI never imposes any restriction on JVM.
JVM can add support to JNI without affecting other
parts of virtual machine.
Native methods accesses JVM features by calling JNI
functions.
Java system overview
Compile-time Environment Compile-time Environment
Java
Byte codes
move locally
or through
network
Java
Source
(.java)
Java
Compiler
Java
Bytecode
(.class )
Java
Interpreter
Just in
Time
Compiler
Runtime System
Class
Loader
Bytecode
Verifier
Java
Class
Libraries
Operating System
Hardware
Java
Virtual
machine
Java compile-time Environment
Java compile-time Environment
Java is platform independent only for the reason:
Only depends on the Java Virtual Machine (JVM),
code is compiled to byte code, which is
interpreted by the resident JVM,
JIT (just in time) compilers attempt to increase
speed.

Java Buzzwords or features of java
Simple
fixes some clumsy features of C++
no pointers
automatic garbage collection
rich pre-defined class library
Object oriented
focus on the data (objects) and methods
manipulating the data
all functions are associated with objects
almost all data types are objects (files, strings, etc.)
potentially better code organization and reuse.

cont...
Interpreted
java compiler generate byte-codes, not native
machine code
The compiled byte-codes are platform-independent
java byte codes are translated on the fly to machine
readable instructions in runtime (Java Virtual
Machine)
Portable
same application runs on all platforms
The sizes of the primitive data types are always the
same.
The libraries define portable interfaces

Cont.
Reliable
Extensive compile-time and runtime error checking.
No pointers but real arrays. Memory corruptions or unauthorized
memory accesses are impossible.
Automatic garbage collection tracks objects usage over time.
Secure
The Java language is secure in that it is very difficult to write incorrect
code or viruses that can corrupt/steal your data, or harm hardware such
as hard disks.
No pointer arithmetic
Garbage collection
Array bounds checking
No illegal data conversions
Browser level (applies to applets only):
No local file I/O


Java achieved protection by confining an applet to java execution
environment and not allowing it access to other parts of the computer.

Multithreaded

multiple concurrent threads of executions can run simultaneously.
utilizes a sophisticated set of synchronization primitives (based on
monitors and condition variables paradigm) to achieve this.
Dynamic

java is designed to adapt to evolving environment
libraries can freely add new methods and instance variables without
any effect on their clients
interfaces promote flexibility and reusability in code by specifying
a set of methods an object can perform, but leaves open how these
methods should be implemented
can check the class type in runtime


Distributed
It handles TCP/IP protocols.
Java also support remote Method Invocation (RMI), this
feature enables a program to invoke methods across a
network.
Architecture- Neutral
The JVM make java write once run any where, any time
forever.
Robustness
The Java language is robust. It has several features designed
to avoid crashes during program execution, including:
Array and string bounds checking
No jumping to bad method addresses
Interfaces and exceptions

First Java Program
Class Example
{
// your program begins with a call to main
Public static void main (string args[])
{
System.out.println (This is a simple Java program);
}
}
Save this program Example.java
Compile with javac Example.java
Execute with java Example
Here class is to declare new class is being defined.
Example is an identifier.
Public static void main (string args[])

Public is access specifier then that member may be accessed by code out
side the class in which it is declared.


Static allows main to be called without having to instantiate a
particular instance of the class. This is necessary since main() is
called by JVM before any objects are made.
void simply tells the compiler that main() does not return a
value.
main() is the method called when a java application begins.
String args [] declares a parameter named args, which is an
array of instances of the class string.
Objects of type string store character strings. In this case args
receives any command-line arguments present when a program
is executed.
System.out.println (This is a simple Java program);

println() is a built-in method for providing out puts, println()
is also used to display other type of information's.
system is a predefined class that provides access to the
system, and out is the output stream that is connected to the
console.
Difference Between C++ and JAVA
Java is (an interpreted) write once, run anywhere
language.

The biggest potential stumbling block is speed:
interpreted Java runs in the range of 20 times
slower than C. But: nothing prevents the Java
language from being compiled and there are just-in-
time (JIT) compilers that offer significant speed-ups.
Write once, run everywhere does (nearly) NEVER
work with C++, but does (nearly) ALWAYS work
with JAVA.

JAVA vs C++
You don't have separated HEADER-files defining class-
properties

You can define elements only within a class.
All method definitions are also defined in the body of the class.
Thus, in C++ it would look like all the functions are inlined (but
theyre not).
File- and classname must be identical in JAVA
Instead of C++ #include you use the import keyword.
For example: import java.awt.*;.
#include does not directly map to import, but it has a similar feel to it

JAVA vs C++
Instead of controlling blocks of declarations like
C++ does, the access specifiers (public, private,
and protected) are placed on each definition for
each member of a class

Without an explicit access specifier, an element defaults to
"friendly," which means that it is accessible to other elements in
the same package (which is a collection of classes being
friends)
The class, and each method within the class, has an access
specifier to determine whether its visible outside the file.

JAVA vs C++
Everything must be in a class.

There are no global functions or global data. If you want the
equivalent of globals, make static methods and static data within
a class.
There are no structs or enumerations or unions, only classes.
Class definitions are roughly the same form in Java as in C++,
but theres no closing semicolon.
Java has no preprocessor.

If you want to use classes in another library, you say import and
the name of the library.
There are no preprocessor-like macros.
There is no conditional compiling (#ifdef)

JAVA vs C++
All the primitive types in Java have specified sizes that
are machine independent for portability.
On some systems this leads to non-optimized performance
The char type uses the international 16-bit Unicode character
set, so it can automatically represent most national characters.

Type-checking and type requirements are much tighter
in Java.
For example:
1. Conditional expressions can be only boolean, not integral.
2. The result of an expression like X + Y must be used; you cant
just say "X + Y" for the side effect.

JAVA vs C++
There are Strings in JAVA
Strings are represented by the String-class, they
arent only some renamed pointers
Static quoted strings are automatically converted into
String objects.
There is no independent static character array string
like there is in C and C++.
There are no Java pointers in the sense of C
and C++

Theres nothing more to say, except that it is a bit
confusing, that a pointerless language like JAVA has
a null-pointer error-message...


JAVA vs C++
Although they look similar, arrays have a very different structure and
behavior in Java than they do in C++.

Theres a read-only length member that tells you how big the array
is, and run-time checking throws an exception if you go out of
bounds.
All arrays are created on the heap, and you can assign one array to
another (the array handle is simply copied).
There is a garbage collection in JAVA
Garbage collection means memory leaks are much harder to cause
in Java, but not impossible. (If you make native method calls that
allocate storage, these are typically not tracked by the garbage
collector.)
The garbage collector is a huge improvement over C++, and makes
a lot of programming problems simply vanish. It might make Java
unsuitable for solving a small subset of problems that cannot
tolerate a garbage collector, but the advantage of a garbage
collector seems to greatly outweigh this potential drawback.

JAVA vs C++
There are no destructors in Java.

There's no need because of garbage collection.
Java uses a singly-rooted hierarchy, so all objects are
ultimately inherited from the root class Object.

The inheritance of properties of different classes is handled by
interfaces.
Java provides the interface keyword, which creates the
equivalent of an abstract base class filled with abstract methods
and with no data members. This makes a clear distinction
between something designed to be just an interface and an
extension of existing functionality via the extends keyword.
Its worth noting that the abstract keyword produces a similar
effect in that you cant create an object of that class.

JAVA vs C++
Java has method overloading, but no operator
overloading.
Java has both kinds of comments like C++ does.
Theres no goto in Java.
The one unconditional jump mechanism is the break
label or continue label, which is used to jump out of
the middle of multiply-nested loops.
Java has built-in support for comment documentation
so the source code file can also contain its own
documentation, which is stripped out and reformatted
into HTML via a separate program. This is a boon for
documentation maintenance and use.
JAVA vs C++
Java contains standard libraries for GUIs

Simple, robust and effective way of creating user-interfaces
Graphical output as part of the language
Java contains standard libraries for solving specific tasks. C++ relies
on non-standard third-party libraries.

These tasks include:
Networking, Database Connection (via JDBC)
Distributed Objects (via RMI and CORBA)
Compression, Commerce
Whatever you want: VoIP, Video-Telephony, MIDI, Games,...
The availability and standard nature of these libraries allow for
more rapid application development.

JAVA vs C++
Generally, Java is more robust, via:
Object handles initialized to null (a keyword).Handles
are always checked and exceptions are thrown for
failures
All array accesses are checked for bounds violations
Automatic garbage collection prevents memory leaks
Clean, relatively full-proof exception handling
Simple language support for multithreading
Bytecode verification of network applets
Standard GUI

Vous aimerez peut-être aussi