Introduction to By Mati Yanko
Primary Goals of Java Portable Secured Object Oriented
Portable – Platform Independent
Secured ByteCode instructions Download through Network Verify Code Run it Network
Object Oriented Programming Encapsulation Inheritance Polymorphism
Object Oriented Programming What is an Object ? Variables (state) Methods (behavior)
static int* buffer; static int counter; void initStack() { buffer = (int*)malloc( sizeof(int) * 10); counter=0; } void freeStack() { free(buffer); } int pop() { return buffer[-- counter]; } void push(int number) { buffer [counter ++]=number; } int main() { initStack(); push(3); pop(); freeStack(); }
public class Stack { private int buffer []; private int counter; Stack() { buffer = new int[10]; counter=0; } void printStack { … } int getCounter() { return counter;} void push (int number) { buffer[counter++] =number; } int pop() { return buffer[ - - counter]; } } Data members (state) Methods (behaviour)
public void main() { Stack myStack=new Stack(); // memory allocation myStack.push(3); myStack.pop(); mystack.pop();// oops }
Object Oriented Programming What is Inheritance ? Stack Stack with Buffer underflow check
public class betterStack extends Stack { int pop() { if (getCounter() >= 0) return super.pop();// superclass method } public void main() { betterStack myStack=new betterStack(); myStack.push(3); myStack.pop(); mystack.pop();// ok }
Object Oriented Programming What is Polymorphism ? Public void main() { Stack myStack;// myStack is null betterStack newStack=new betterStack(); myStack = newStack; myStack.pop() ; // ??? }
C vs. Java Macros Low-level memory management Pointer arithmetic p++ Casts Free Unsafe arrays Stack allocation Big and small values Object Oriented Dynamic class loading References instead of arbitrary pointers Heap only No free (Garbage collection) Exceptions
References Java programming Language / SUN