Вы находитесь на странице: 1из 26

Objectives

To be able to:
To be able to differentiate between Structured Oriented
Programming and OOP.
To be able to understand the difference between two
object oriented languages like C++ and JAVA.
To be able to understand the data storage in JVM.
To be able to understand the PATH, CLASSPATH setting.

THE WAY JAVA WORKS


SOURCE -> Compiler
->Output code->Virtual
Machines

WHAT WE DO IN JAVA

Object Oriented Principles


Abstraction Hiding the underlying
implementation
Inheritance Reusing code (
base code)
Polymorphism Changing behaviour
Encapsulation Getters and setters
(Hiding variables or state of objects)

Procedural Vs. Object Oriented


Programming
Procedural Programming
top down design (module ->2 sub-modules-> 3sub-modules..n submodules)
create functions to do small tasks
communicate by parameters and return values
Object-Oriented Programming
design and represent objects
Bottom up design (n classes->create objects for communication
between classes)
determine relationships between objects
determine attributes each object has
determine behaviors' each object will respond to
create objects and send messages to them to use or manipulate their
attributes

Overview

JDBC

Java - Object-Oriented Programming


Class:
A class is a blueprint, based on which Objects are
constructed.
Object :
An object is an instance of a class. Basically, we
can have any number of objects constructed
from a class with same set of properties
(behaviour)
That is every object of the class has identical
state and behaviour.

Sample HelloWorld App


// HelloWorld.java
public class HelloWorld {
public static void main (String args[]){
System.out.println(Hello World!!);
}
}

How to Set PATH ENV?

IS JAVA WORKING ?

Our Target
Compiling and Running HelloWorld

Compiling HelloWorld.java
javac HelloWorld.java
Running an application
java HelloWorld

Note: Before compile and run the JAVA code need to set the PATH
and CLASSPATH environment variables.

TestGreeting.java
1//
2//Sample"HelloWorld"application
3//
4publicclassTestGreeting{
5publicstaticvoidmain(Stringargs[]){
6Greetinghello=newGreeting("Hello");
7hello.greet("World");
8}
9}
Greeting.java
1//TheGreetingclassdeclaration
2publicclassGreeting{
3privateStringsalutation;
4
5publicGreeting(Strings){
6salutation=s;
7}
8
9publicvoidgreet(Stringwhom){
10System.out.println(salutation+""+whom);
11}
12}

Java Virtual Machine

Most programming languages compile source code directly into machine


code, suitable for execution on a particular microprocessor architecture.

The difference with Java is that it uses bytecode - a special type of machine
code thats why JAVA is compiled and interpreted type of language.

Java bytecode executes on a special type of microprocessor.

Strangely enough, there wasn't a hardware implementation of this


microprocessor available when Java was first released. Instead, the
processor architecture is emulated by what is known as a "virtual machine".

Whole JVM is written in C. Suns JVM are the called


HotSpot JVM,
OpenJDK

Note: For more flavors of Virtual Machines go through with:


http://en.wikipedia.org/wiki/Java_Virtual_Machine

JVM Architecture

Where storage lives


The Stack:
The local variables and arguments of a function are allocated
space from the stack.
Though storage for object references may exist on the stack, Java
objects themselves are not placed on the stack.

The Heap:
All Java objects take their storage from the heap
Whenever you need to create an object, you simply write the code
to create it by using new, and the storage is allocated on the heap
when that code is executed.

Where Storage Lives


Method area
Class file Instructions are stored in the
method area.
Static storage contains data that is
available for the entire time a program
is running.
Static keyword is used to specify that a
particular variable is static.
Static storage is in method area.

JVM Architecture

Class Loader

Byte-Code verifier

JVM Architecture

Adaptive Optimizer (interpreter)

JIT Compiler (Compiler + interpreter)


JIT Compiler uses
1. Buffering of function machinecode
2. Execution on demand

JVM Architecture

Garbage Collection
Java has a garbage collector, which looks at all the

objects that were created with new and figures out which
ones are not being referenced anymore. Then it releases
the memory for those objects, so the memory can be
used for new objects.

This means that you never need to worry about freeing

memory yourself.

This eliminates a certain class of programming problem:

the so-called memory leak, in which a programmer


forgets to release memory.

This algorithm is based in mark and seep.

Garbage Collection
Garbage Collection Algorithm
The algorithm performs two basic things:
1. Detect garbage objects.
(maintain a tree of objects)
2. To reclaim heap space.
Some functions available for requesting the garbage
collection: (Gives a suggestion to JVM)
Runtime.getRuntime().gc().
System.gc();
System.gc() simply calls Runtime.getRuntime().gc(). And
Runtime.getRuntime().gc() invokes a native method in the JVM.

Features of all Java Versions


Version 1.0:
Netscape 2.0-4.0 included Java 1.0.
Microsoft and other companies licensed Java.

Version 1.1:
Improvements include better event handling, inner classes,
improved JVM.
Microsoft developed its own 1.1. compatible Java Virtual
Machine for the Internet Explorer.
Many browsers in use are still compatible only with 1.1.
Swing packages of greatly improved graphics became
available during this time but not included with the core
language.

Version 1.2, also called the Java 2 Platform


Code and tools distributed as The Software
Development Kit (SDK)
Java Foundation Classes (JFC), based on Swing, for
improved graphics and user interfaces, now included
with the core language.
Collections API included support for various lists, sets,
and hash maps.

Version 1.3:
Performance enhancements including the Hotspot
virtual machine.

Version 5.0:

Faster startup and smaller memory footprint


Metadata
Formatted output
Generics
Improved multithreading features
Annotations

version 6.0:

Pluggable Annotation Processing API (JSR 269)


Common Annotations (JSR 250)
Java API for XML Based Web Services - 2.0 (JSR 224)
JAXB 2.0 (JSR 222)
Web Services Metadata (JSR 181)
Streaming API for XML (JSR 173)
XML Digital Signature (JSR 105)
Java Class File Specification Update (JSR 202)
Java Compiler API (JSR 199)
JDBC 4.0 (JSR 221)
Scripting in the Java Platform (JSR 223)

Assignment
Write a Java class that can print Welcome to
Java Programming
Learn the difference between PATH, CLASSPATH,
JAVA_HOME Environment variables and be able
to set them up.
Explain the importance of JVM in JAVA
and its architecture.
Modularize a JAVA class and explain its meaning.
Learn and point out why we moved from
structured programming to OOP.

Вам также может понравиться