Академический Документы
Профессиональный Документы
Культура Документы
H22
1. Algorithms and Data 2. Java
– Useful because commonly
Structures used
– From simple to more – Great example of an object-
complex oriented language
– All very fundamental and • Object-oriented languages
useful encourage code reuse and
– You practice algorithmtic good code design
thinking… => Code tends to be more
reliable
• Java is also “strongly
typed” so it’s harder to
make mistakes
– Learning how computers
work: how programs are
compiled and executed.
Bill Joy
• BSD Unix guy from Berkeley James Gosling
• founded Sun Microsystems • early fame as the author
(early 80s) of “Gosling Emacs” (killed
• “the network is the computer” by GNU)
(a little ahead of its time) • then onto Sun’s “NeWS”
– target workstation market window system (killed by
• missed the boat on PC X)
revolution, retreated to Aspen, • lesson: keeping things
Colorado proprietary is kiss of death
Java Programming Language: History
• Joy and Gosling joined force, Sun subsidiary,
FirstPerson, Inc. (1992)
• target consumer electronics: PDAs, appliances, phones,
all with cheap infra-red kinds of networks
• need a language that’s small, robust, safe, secure, wired
– started working on C++--
– soon gave up hope, decided to start from scratch
• Bad luck (again)
• bad luck: a little ahead of time
– PDAs died with the demise of Apple Newton
– switched to interactive TV (ITV)
– the resulting language was called “Oak”
– then ITV died too
• good luck (finally)
– the net exploded in 1993
– Oak became Java, rest is history!
Learning Java
• just like learning any new language
– syntax: “new words”
– grammar: how to put them together
– programming: telling a coherent story
– library: use plots already written
Java compiler
Java bytecode
Machine code
Related Topic: Programming
Errors…
A program can have three types of errors
• The compiler will find problems with syntax and
other basic issues (compile-time errors)
– If compile-time errors exist, an executable version of
the program is not created
• A problem can occur during program execution,
such as trying to divide by zero, which causes a
program to terminate abnormally (run-time
errors)
• A program may run, but produce incorrect results
(logical errors)
Java Programming: The Edit/Compile/Run
Loop
• Programming in Java
consists of three tasks
– edit java source code (.java
files)
– compile java source code to
generate bytecode (.class
files)
– execute/run/test bytecode
using an interpreter
An Sample Java Program
//==========================================================
//
// HelloWorld.java
//
// Author: Richard Yang
//
// Class: HelloWorld
//
// ---------------------------------------------------------
// This program prints a string called "Hello World!”
//
//==========================================================
class header
}
Java Method and Statements
• Methods
– building blocks of a class
– each method name is an identifier, e.g. “addStudents”
• convention we follow: a method name starts lower case, with
each additional English word capitalized (e.g., main,
doMyJob )
– the main method
• each Java application must have one
• all programs start by executing the main method
– braces are used to start ({) and end (}) a method
• Statements
– every statement must end in a semicolon ;
Classes and Methods
• Methods and classes are language constructs to
help organize a program
– a method “organizes” a sequence of statements
• thus we can use a single method to refer to a bunch of
statements
– a class “organizes” a collection of methods (and data)
• Division of tasks into classes and methods
supports object-oriented programming and
creates good abstraction boundaries between
different tasks and objects
Classes and Methods Provide
Abstraction
• In the jargon of program design, they are
structures which provide abstraction
• The objective of abstraction is to help
programmers to hide (or ignore) the right details
at the right time
• Why abstraction
– a human being can only manage seven (plus or
minus 2) pieces of information at one time
• if we group information into chunks, for example, by
organizing complex software carefully into methods and
classes, we can write more complex software
Methods and Classes
• A method provides an abstraction for a sequence of
statements
– initially, a method helps a programmer to refer to a a sequence
of statements which she may reuse
– more generally, a method defines a service
• a method takes input (parameters), performs some actions, and
(sometime) returns a value
– we invoke a method (call its service) and specify
input/parameters
• A class provides an abstraction for objects with some
common behaviors/services which we can tell them to
perform for us
– the services/behaviors provided by an object are defined by the
methods in the class that defines the object
– we create objects from its class definition: class serves as a
blueprint
Example
• System.out is an object created from the
PrintStream class
• Example: invoking the println method
of the System.out object:
Object method
Information provided to the method
created dot
(parameters)
from
PrintStream
class 29
Declaring Object Reference Variables
• In the general case, we need to first create
objects
– pre-created objects such as System.out are rare
• To “keep track” of created objects, we need
variables to refer to the created objects
– variables are identifiers
• A variable can be used to refer to different
objects
• To avoid mixing different types of objects, we
should specify the type of objects that a variable
can refer to; thus we need to specify the type of
a variable:
<Type> <variableName>;
Declaring Object Reference Variables
• Example:
String title;
where String is a predefined class in
Java; we also call title an object
reference variable
• No object has been created with the
above declaration of a variable
• The object itself must be created
separately
Explicitly Creating Objects
• We use the new operator to create an
object
title = new String (“Java Software Solutions");
• Once an object has been instantiated, we can use the dot operator
to invoke its methods, e.g.,
count = title.length();