cs 331 - 4/23/081 a quick and caffeinated overview presented by chris start o bject oriented...

24
CS 331 - 4/23/08 1 A Quick and Caffeinated Overview Presented by Chris Start Object Oriented Programming Languages:

Upload: ellen-pearson

Post on 11-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 331 - 4/23/081 A Quick and Caffeinated Overview Presented by Chris Start O bject Oriented Programming Languages:

CS 331 - 4/23/08 1

A Quick and Caffeinated Overview

Presented by Chris Start

Object Oriented Programming Languages:

Page 2: CS 331 - 4/23/081 A Quick and Caffeinated Overview Presented by Chris Start O bject Oriented Programming Languages:

CS 331 - 4/23/08 2

Background

Who made it? : James Gosling Working for? : Sun Microsystems since 1984 When? : in June 1991 Originally called: Oak, Green, then Java

(from list of random words) Why? (originally):

needed a safe, reliable language for consumer electronics (toasters, TVs, microwave ovens)

C was not object oriented, which was necessary C++ was too large/complex and unreliable

Products never marketed In 1993 w/explosion of WWW and graphical

browsers, Java was found useful for Web Applets embedded into HTML code

Page 3: CS 331 - 4/23/081 A Quick and Caffeinated Overview Presented by Chris Start O bject Oriented Programming Languages:

CS 331 - 4/23/08 3

Different Versions for Different Applications

Java 1 was a single version for everyone

Java 2 : J2EE, J2ME, J2SE, built for different types of platforms Java EE: Enterprise Edition for large “enterprise” applications

Java ME: Mobile Edition was very stripped down for mobile applications

Java SE: Standard Edition for most users

Page 4: CS 331 - 4/23/081 A Quick and Caffeinated Overview Presented by Chris Start O bject Oriented Programming Languages:

CS 331 - 4/23/08 4

History Timeline JDK 1.0 (January 23, 1996) JDK 1.1 (February 19, 1997)

inner classes added to the language (nested classes, classes completely defined within the body of another class)

JavaBeans reusable software components - manipulated visually in a builder tool

group objects to be moved together J2SE 1.2 (December 8, 1998)

Swing graphical API integrated into core classes

Sun's JVM included w/JIT compiler J2SE 1.3 (May 8, 2000)

Page 5: CS 331 - 4/23/081 A Quick and Caffeinated Overview Presented by Chris Start O bject Oriented Programming Languages:

CS 331 - 4/23/08 5

More History… J2SE 1.4 (February 6, 2002)

first release of the Java platform developed under the Java Community Process

assert keyword Java Web Start included (Java Web Start was first released in March, 2001 for J2SE 1.3)

J2SE 5.0 (September 30, 2004) Java SE 6 (December 11, 2006)

Updates 2 & 3 released during 2007 Updates 4 & 5 released in 2008

Dramatic performance improvements for the core platform

Page 6: CS 331 - 4/23/081 A Quick and Caffeinated Overview Presented by Chris Start O bject Oriented Programming Languages:

CS 331 - 4/23/08 6

Not Historical… Yet

Java SE 7 early planning and development stages started up in August 2006; to be tentatively released in January 2009

JVM support for dynamic languages, following the prototyping work currently done on the Multi Language Virtual Machine

new library for parallel computing on Multi-core processors

Page 7: CS 331 - 4/23/081 A Quick and Caffeinated Overview Presented by Chris Start O bject Oriented Programming Languages:

CS 331 - 4/23/08 7

Syntax:

Mostly derived from C++, but is exclusively object-oriented (unlike C++ which is structured, generic, and OOP)

Almost everything is an object and all code is inside a class Numbers, Boolean values, characters are not objects for performance reasons

Page 8: CS 331 - 4/23/081 A Quick and Caffeinated Overview Presented by Chris Start O bject Oriented Programming Languages:

CS 331 - 4/23/08 8

Java Characteristics…

Predefined container classes (ArrayList) can only contain objects

Primitive type (int) must be placed in a wrapper class Pre Java 5.0

myArray.add(new Integer(10)); Post Java 5.0

myArray.add(10); boxing (post Java5)implicitly converts primitive types to objects when put in object context

Page 9: CS 331 - 4/23/081 A Quick and Caffeinated Overview Presented by Chris Start O bject Oriented Programming Languages:

CS 331 - 4/23/08 9

Hello, World example:

1. Code is saved as HelloWorldApp.java

2. Javac compiler converts to bytecode producing HelloWorldApp.class

3. HelloWorldApp.class is launched and run

Used JDK6 Update 6

Page 10: CS 331 - 4/23/081 A Quick and Caffeinated Overview Presented by Chris Start O bject Oriented Programming Languages:

10CS 331 - 4/23/08

Philosophy

5 Primary goals: use object-oriented programming methodology

platform independence contain built-in support for computer networks

designed to execute code from remote sources securely

easy and safe to use

Page 11: CS 331 - 4/23/081 A Quick and Caffeinated Overview Presented by Chris Start O bject Oriented Programming Languages:

11CS 331 - 4/23/08

Platform Independence able to write code, compile it once, and run on

any platform/system

How?Virtual Machines and the JIT compiler

Java compilers convert Java Language to bytecode (simplified Virtual Machine instructions)

Virtual Machine, written in native code for host hardware, interprets bytecode for execution

Page 12: CS 331 - 4/23/081 A Quick and Caffeinated Overview Presented by Chris Start O bject Oriented Programming Languages:

12CS 331 - 4/23/08

JIT: Just In Time Compiler Vs.

Static Compilation

The Java bytecode is interpreted/converted to native machine code by the JIT compiler.

Early versions of JIT ran slow compared to code natively compiled to machine code – got reputation of running slowly

Translates the Java language code directly into native machine code No bytecode stage Good performance compared to original JIT

Lose portability (not cross platform anymore!)

Can be useful to provide a bytecode and a faster, optimized native version

Page 13: CS 331 - 4/23/081 A Quick and Caffeinated Overview Presented by Chris Start O bject Oriented Programming Languages:

13CS 331 - 4/23/08

Dynamic Compilation (Sophisticated VM)

VM can optimize and recompile critical parts of the program

Can be faster than static compilation, recompiles code based on what is happening

Combination of JIT and dynamic compilation take advantage of speed without losing portability

Page 14: CS 331 - 4/23/081 A Quick and Caffeinated Overview Presented by Chris Start O bject Oriented Programming Languages:

CS 331 - 4/23/08 14

Java’s Memory Management

All Java objects are explicit heap-dynamic Allocated with new but no deallocation

Ex.: myArray.add(new Integer(10)); No Pointers!

Everything done by reference No access to hardware No access to absolute memory addresses No function pointers to call functions dynamically

Virtual functions with inheritance to get dynamic binding of member to objects

Page 15: CS 331 - 4/23/081 A Quick and Caffeinated Overview Presented by Chris Start O bject Oriented Programming Languages:

CS 331 - 4/23/08 15

Automatic Memory Management : Garbage Collection

Users don’t have to deallocate Java runtime responsible for deallocation from heap

Lack of references to an object allows object to be freed

Memory leaks can still happen if user keeps references to variables that they no longer need/use

Will not free resources other than heap memory Ex.: files, lock on a shared resource…

finalize method similar to C++ destructor Called when garbage collector is about to free resource

Page 16: CS 331 - 4/23/081 A Quick and Caffeinated Overview Presented by Chris Start O bject Oriented Programming Languages:

CS 331 - 4/23/08 16

Comparing to C++ : Simplicity

Unlike C++, Java suppresses several features (such as operator overloading and multiple inheritance) for classes to simplify the language to "save the programmers from themselves"

to prevent possible errors and anti-pattern design

Page 17: CS 331 - 4/23/081 A Quick and Caffeinated Overview Presented by Chris Start O bject Oriented Programming Languages:

CS 331 - 4/23/08 17

Inheritance

Supports single inheritance final methods cannot be overridden in descendant classes Bindings can be statically bound to methods of subclass

Abstract class: interface

Page 18: CS 331 - 4/23/081 A Quick and Caffeinated Overview Presented by Chris Start O bject Oriented Programming Languages:

CS 331 - 4/23/08 18

Abstract class: interface

Partial support for multiple inheritance No constructors or nonabstract methods Only method declarations (not definitions) and

named constants Not inherited, implemented by class

All methods must be implemented if defined by interface

“virtually” takes place of second parent class

public interface Comparable {public int compareTo(Object b);

}

Page 19: CS 331 - 4/23/081 A Quick and Caffeinated Overview Presented by Chris Start O bject Oriented Programming Languages:

CS 331 - 4/23/08 19

Dynamic Binding

In C++ a method must be defined as virtual to allow dynamic binding

In Java, all methods are dynamic unless defined as final

Allows easier integration and logical flow in inheritance structure and deployment

Methods defined as static or private use static binding and disallow overriding

Page 20: CS 331 - 4/23/081 A Quick and Caffeinated Overview Presented by Chris Start O bject Oriented Programming Languages:

CS 331 - 4/23/08 20

Nested Classes

Hidden from all classes in their package, except for nesting class

Nonstatic classes nested directly in another class have implicit pointer to nesting class Gives access of all nesting class’s methods to nested class

Static classes do not have pointer, cannot access members of nesting class Static nested classes are like C++’s nested classes

Page 21: CS 331 - 4/23/081 A Quick and Caffeinated Overview Presented by Chris Start O bject Oriented Programming Languages:

CS 331 - 4/23/08 21

History tidbit: Implementations

Microsoft used to ship a version of Java Virtual Machine (MSJVM)Had proprietary components which if used would not run on Sun’s JVM

Sun sued MS for breach of trademarkSince October 2001, Java is not shipped w/Windows, installed separately

Page 22: CS 331 - 4/23/081 A Quick and Caffeinated Overview Presented by Chris Start O bject Oriented Programming Languages:

22CS 331 - 4/23/08

Conclusions :

Started as a language to program toasters (microwave ovens, TVs, etc)

Became popular because: Portable (especially across WWW) Easy to use (programmer’s like its design)

Free and easy to obtain Relatively safe to program in (less reliance on the expertise of the programmer)

Page 23: CS 331 - 4/23/081 A Quick and Caffeinated Overview Presented by Chris Start O bject Oriented Programming Languages:

23CS 331 - 4/23/08

Conclusions Continued…

More consistent to principles of object-oriented programming than C++

Lack of functions = does not support procedural programming

Dynamic binding is the “normal” way to bind method calls to method definitions

Uses interfaces as simple form of support for multiple inheritance

Page 24: CS 331 - 4/23/081 A Quick and Caffeinated Overview Presented by Chris Start O bject Oriented Programming Languages:

24CS 331 - 4/23/08

References :

http://java.sun.com/ http://en.wikipedia.org/wiki/ Sebesta, Programming Languages. 8th Edition.

http://math.hws.edu/javanotes