Skip to main content

Posts

Showing posts from August, 2015

History of Java

1990 Oak (interactive television, big failure) 1994 Java (for the Internet) Main feature: "Write Once, Run Any Where" => wrap the operating system so they all look the same Designed for A fresh start (no backward compatibility) “Pure” OOP: C++ Syntax, Smalltalk style Improvements over C++ much harder to write a bad program  Internet programming Very hard to create a virus  Run in a web browser (and at the server) There is a speed issue (from Java 1.3 and up much better) C# Microsoft's “Java-Killer” project release 2001 Language very similar to Java   Commen-Language Runtime (CLR) supports 30+ languages  

Java Program Structure

Code Example, Revisited

Polymorphism and Dynamic Binding

Polymorphism: One piece of code works with all shape objects. Dynamic binding: How polymorphism is implemented Take previous Shape class hierarchy  remove inheritance  remove general and abstract class Shape 

Generalization and Specialization

Generalization creates a concept with a broader scope. Specialization creates a concept with a narrower scope. Reusing the interface!  Inheritance: get the interface from the general class. Objects related by inheritance are all of the same type.

Aggregation and Decomposition

Idea: make new objects by combining existing objects. Reusing the implementation!  An aggregation consists of a number of (sub-)concepts which collectively is considered a new concept. A decomposition splits a single concept into a number of (sub-)concepts.

Phenomenon and Concept

A phenomenon is a thing in the “real” world that has individual existence. an object A concept is a generalization, derived from a set of phenomena and based on the common properties of these phenomena. a class Characteristics of a concept  A name Intension, the set of properties of the phenomenon  Extension, the set of phenomena covered by the concept  Classification and Exemplification, Examples • hat, 23, 34, mouse, telephone, book, 98, 45.34, hello numbers: 23, 34, 98, 45.34  words: hat, mouse, telephone, book, hello mouse, tyrannosaurus rex, allosaurus, elephant, velociraptor dinosaur: tyrannosaurus rex, allosaurus, velociraptor mammal: mouse, elephant A classification is a description of which phenomena that belongs to a concept. An exemplification is a phenomenon that covers the concept  

Encapsulation and Information Hiding

Data can be encapsulated such that it is invisible to the “outside world”. Data can only be accessed via methods. What the “outside world” cannot see it cannot depend on! The object is a “fire-wall” between the object and the “outside world”. The hidden data and methods can be changed without affecting the “outside world”. 

The Class Concept

A class is a collection of objects (or values) and a corresponding set of methods.  A class encapsulates the data representation and makes data access possible at a higher level of abstraction. Example 1: A set of vehicles with operations for starting, stopping, driving, get km/liter, etc. Example 2: A time interval, start time, end time, duration, overlapping intervals, etc. Example 3: A string, upper case, compare, lower case, etc.  str.equals(otherStr) – class/Java style   strcmp(str, otherStr) – C style

The Object Concept

An object is an encapsulation of data.  An object has identity (a unique reference)  social security number (cpr), employee number, passport number state, also called characteristics (variables)  hungry, sad, drunk, running, alive  behavior (methods) eat, drink, wave, smile, kiss  An object is an instance of an class   A class is often called an Abstract Data Type (ADT).

Introduction to Object-Oriented Programming

Objects and classes  Encapsulation and information hiding    Mental exercises            Classification and exemplification           Aggregation and decomposition           Generalization and specialization • Inheritance  • Polymorphism and dynamic binding  • Java an example of an object-oriented programming language          Program example         History of Java         Comparison to C/C+

interface Java Keyword

The interface keyword is used to declare a new Java interface, which is a collection of methods. Interfaces are a powerful feature of the Java language. Any class may declare that it implements one or more interfaces, meaining it implements all of the methods defined in those interfaces. Examples public interface IPolygon { public float getArea(); public int getNumberOfSides(); public int getCircumference(); }

import Java Keyword

The import keyword makes one class or all classes in a package visible in the current Java source file. Imported classes can be referened without the use of fully−qualified class names. Examples import java.io.File; import java.net.*;

implements Java Keyword

The implements keyword is used in a class declaration to indicate that the class being declared provides implementations for all methods declared in the interface whose name follows the implements keyword. Examples public class Truck implements IVehicle { }

if Java Keyword

The if keyword indicates conditional execution of a block. The condition must evaluate to a boolean value. Examples if (condition) { <statements> } if (condition) { <statements> } else { <statements> }

float Java Keyword

float is a Java primitive type. A float variable may store a single−precision floating point value. Examples float ratio = .01; float diameter = 6.15; float height = 1.35E03; // 1.35 * 103 or 1350.0 float height = 1e−2; // 1.0 * 10−2 or 0.01

finally Java Keyword

The finally keyword is used to define a block that is always executed in a try−catch−finally statement. A finally block typically contains cleanup code that recovers from partial execution of a try block. Examples try { <block that may throw exceptions> } catch (<java.lang.Exception or subclass> e) { <code to handle exception e> } finally { <statements that execute with or without exception> }

extends Java Keyword

The extends keyword is used in a class or interface declaration to indicate that the class or interface being declared is a subclass of the class or interface whose name follows the extends keyword. Examples public class Rectangle extends Polygon { }

else Java Keyword

The else keyword is always used in association with the if keyword in an if−else statement. The else clause is optional and is executed if the if condition is false. Examples if (condition) { <statements> } else { <statements> }

double Java Keyword

double is a Java primitive type. A double variable may store a double−precision floating point value. Examples double ratio = .01; double diameter = 6.15; double height = 1.35E03; // 1.35 * 103 or 1350.0 double height = 1e−2; // 1.0 * 10−2 or 0.01

default Java Keyword

The default keyword is used to label the default branch in a switch statement. Examples int arg = <some value>; switch (arg) { case 1: <statements> break; case 2: <statements> break; default: <statements> break; }

class Java Keyword

The class keyword is used to declare a new Java class, which is a collection of related variables and/or methods. Classes are the basic building blocks of object−oriented programming. A class typically represents some real−world entity such as a geometric Shape or a Person. A class is a template for an object. Every object is an instance of a class. To use a class, you instantiate an object of the class, typically with the new operator, then call the classes methods to access the features of the class. Examples public class Rectangle { float width; float height; public Rectangle(float w, float h) { width = w; height = h; } public float getWidth() { return width; } public float getHeight() { return height; } }

catch Java Keyword

The catch keyword is used to define exception handling blocks in try−catch or try−catch−finally statements. Examples try { <block that may throw exceptions> } catch (<java.lang.Exception or subclass> e) { <code to handle exception e> } try { <block that may throw different exceptions> } catch (FooException e) { <code to handle FooException e> } catch (BarException e) { <code to handle BarException e> } try { <block that may throw exceptions> } catch (<java.lang.Exception or subclass> e) { <code to handle exception e> } finally { <statements that execute with or without exception> }

case Java Keyword

The case is used to label each branch in a switch statement. Examples int arg = <some value>; switch (arg) { case 1: <statements> break; case 2: <statements> break; default: <statements> break; }

break Java Keyword

The break keyword is used to prematurely exit a for, while, or do loop or to mark the end of a case block in a switch statement. Examples for (i=0; i<max; i++) { if (<loop finished early>) { break; } } int type = <some value>; switch (type) { case 1: <statement> break; case 2: <statement> break; default: <statement> }

abstract Java Keyword

The abstract keyword may modify a class or a method. An abstract class can be extended (subclassed) but cannot be instantiated directly. An abstract method is not implemented in the class in which it is declared, but must be overridden in some subclass. Examples public abstract class MyClass { } public abstract String myMethod();

Java Language Keywords

Here is a list of keywords in the Java programming language. You cannot use any of the following as identifiers in your programs. The keywords const and goto are reserved, even though they are not currently used. true, false, and null might seem like keywords, but they are actually literals; you cannot use them as identifiers in your programs. The abstract Keyword  The boolean Keyword  The break Keyword  The byte Keyword   The case Keyword  The catch Keyword   The char Keyword  The class Keyword   The continue Keyword   The default Keyword  The do Keyword  The double Keyword  The else Keyword  The extends Keyword   The false Keyword  The final Keyword  The finally Keyword The float Keyword  The for Keyword   The if Keyword  The implements Keyword The import Keyword  The instanceof Keyword   The int Keyword  The interface Keywo...

What is Java technology and why do I need it?

Java is a programming language and computing platform first released by Sun Microsystems in 1995. There are lots of applications and websites that will not work unless you have Java installed, and more are created every day. Java is fast, secure, and reliable. From laptops to datacenters, game consoles to scientific supercomputers, cell phones to the Internet, Java is everywhere! Is Java free to download? Yes, Java is free to download. Get the latest version at  java.com . If you are building an embedded or consumer device and would like to include Java, please  contact Oracle  for more information on including Java in your device. Why should I upgrade to the latest Java version? The latest Java version contains important enhancements to improve performance, stability and security of the Java applications that run on your machine. Installing this free update will ensure that your Java applications continue to run safely and efficiently.