Sunteți pe pagina 1din 18

Procedural vs.

Object-Oriented Programming

Procedural Programming
top down design(module->2 sub-modules-> 3sub-modules..n sub-modules)
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


Classes as Blueprints for Objects or

A class is a set of objects with the same properties.


In manufacturing, a blueprint is a description of a
device from which many physical devices are
Constructed
In software, a class is a description of an object:
- A class describes the data that each object includes
- A class describes the behaviors that each object
exhibits
In Java, classes support four key features of OOP:
Encapsulation (data hiding, separation of concern)
Inheritance (reusability)
Polymorphism (compile time polymorphism and runtime polymorphism)
Dynamic Binding (function overriding)
3

Java - Object-Oriented Programming


Java unlike C++?
- Features removed which make C++ unsafe:

pointer arithmetic, global variables, standalone functions (everything is a


method), friend functions (Everything in a package is a friend of everything
else in the package.) and non-virtual functions.

- Features removed which make it hard to read:


#define, typedef, operator overloading, unions and structs.

- Features added to Java:


Arrays with bounds checking, garbage collection, concurrency, interfaces
and packages. There is no need to explicitly free memory in Java.

A first program in Java


A simple first program: HelloWorld.java
Edit, Compile and Run phases of the program
HelloWorld.java
// Sample "Hello World" application
public class HelloWorld {
public static void main (String args[]) {
System.out.println ("Hello World");
}
}

Basic Java Application: Compile & Run


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.

Basic Java Application: TestGreeting with Constructor


TestGreeting.java
1 //
2 // Sample "Hello World" application
3 //
4 public class TestGreeting{
5 public static void main (String args[]) {
6 Greeting hello = new Greeting("Hello");
7 hello.greet("World");
8 }
9 }
Greeting.java
1 // The Greeting class declaration
2 public class Greeting {
3 private String salutation;
4
5 public Greeting(String s) {
6 salutation = s;
7 }
8
9 public void greet(String whom) {
10 System.out.println(salutation + " " + whom);
11 }
12 }
7

Java Platform

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

10

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.

11

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.

12

Objects are manipulated with references


Any object is manipulated with the help of a reference
If there is a reference, it doesnt mean theres necessarily an object
connected to it
E.g. Suppose you create a String reference:
String s;
But here only the reference is created, not the object. So if a method is
called using this reference, there will be a compile time error (Java
compiler catches if the value of an un-initialized variable is read)
When you create a reference, you want to connect it with a new object. This
is done with the help of the new keyword
String s = new String(); or
String s = new String("asdf");

13

Objects are manipulated with references


The second statement not only creates a new String
but it also gives information about how to make the
String by supplying an initial character string

14

JVM Architecture

Class Loader

Byte-Code verifier

15

JVM Architecture

Adaptive Optimizer (interpreter)


JIT Compiler (Compiler + interpreter)
JIT Compiler uses

1. Buffering of function machine-code


2. Execution on demand

16

JVM Architecture

17

Thanks

S-ar putea să vă placă și