Chapter One: Introduction • To understand the activity of

8/18/2011
Chapter Goals
• To understand the activity of programming
• To learn about the architecture of computers
• To learn about machine code and high level programming
languages
• To become familiar with your computing environment and your
compiler
• To compile and run your first Java program
• To recognize syntax and logic errors
Chapter One: Introduction
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Prerequisites
What Is Programming?
• Computer savvy (file management, text editing)
• Computers are programmed to perform tasks
• Problem solving skills
• Different tasks = different programs
• Time management
• Program
• High school math (algebra, trigonometry)
• No prior programming background required
• Sequence of basic operations executed in succession
• Contains instruction sequences for all tasks it can execute
• Sophisticated programs require teams of highly skilled
programmers and other professionals
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Self Check 1.1
Self Check 1.1
What is required to play a music CD on a computer?
What is required to play a music CD on a computer?
Answer: A program that reads the data on the CD and sends
output to the speakers and the screen.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
1
8/18/2011
Self Check 1.2
Self Check 1.2
Why is a CD player less flexible than a computer?
Why is a CD player less flexible than a computer?
Answer: A CD player can do one thing – play music CDs. It
cannot execute programs.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Self Check 1.3
Self Check 1.3
Can a computer program develop the initiative to execute tasks in
a better way than its programmers envisioned?
Can a computer program develop the initiative to execute tasks in
a better way than its programmers envisioned?
Answer: No – the program simply executes the instruction
sequences that the programmers have prepared in advance.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
The Anatomy of a Computer
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Central Processing Unit
• Central processing unit
• Chip
• Transistors
• Storage
• Primary storage: Random-access memory (RAM)
• Secondary storage: e.g. hard disk
• Removable storage devices: e.g.: floppy disks, tapes, CDs
• Peripherals
• Executes very simple instructions
• Executes instructions very rapidly
• General purpose device
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
2
8/18/2011
A Memory Module with Memory Chips
A Hard Disk
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
A Motherboard
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Schematic Diagram of a Computer
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
The ENIAC
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Self Check 1.4
Where is a program stored when it is not currently running?
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
3
8/18/2011
Self Check 1.4
Self Check 1.5
Where is a program stored when it is not currently running?
Which part of the computer carries out arithmetic operations, such
as addition and multiplication?
Answer: In secondary storage, typically a hard disk.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Self Check 1.5
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Important Characteristics of a Computer
Which part of the computer carries out arithmetic operations, such
as addition and multiplication?
Answer: The central processing unit.
• Can store large amount of data
– Instructions to be executed (programs)
– Data to operate with
• Can execute very simple instructions from a
predetermined set of possible instructions
– Access particular data in memory (read)
– Store data in memory (write)
– Operate with data in CPU
• Perform logical/arithmetic operations
– Make data available to an external source
– Get data from an external source
• Executes instructions fast – millions per second…
• Instructions must be provided in native language
(machine language)
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Machine Instructions
• Need to be coded following a very specific format
(machine language)
– Very detailed
– Each instruction performs a simple task
• General categories of machine instructions:
– Control instructions – tell the computer what to do next
– Data handling instructions – operate on data stored, or to be
stored, in the computer’s memory
•
•
•
•
•
•
Alter data
Compute new data
Write data to memory
Read data from memory
Write data to an external device (output)
Read data from an external device (input)
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Instruction Execution
• Instruction is read from memory
• Instruction is placed in special register in CPU
• Signals are initiated to activate required
computer components to perform a particular
task
– These correspond to the purpose of the instruction
• Instructions are execute sequentially (except
in parallel architectures)
– Only one instruction at a time
– When an instruction is finished, the next instruction
in the sequence is executed
• Except in the case of control instructions that may alter
the sequence of instructions to execute.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
4
8/18/2011
Machine Code
• Java Virtual Machine (JVM) – a typical sequence of machine
instructions is:
Common Approach with High-Level
Language Programs
1. Load the contents of memory location 40.
2. Load the value 100.
3. If the first value is greater than the second value, continue with the
instruction that is stored in memory location 240.
Program P in
language L
Compiling
process
• Machine instructions are encoded as numbers:
21 40
16 100
163 240
produces
• Compiler translates high-level language to machine code
(if no grammar
errors)
Machine Code
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Approach Followed by Java System
Program P in
language Java
Compiling
process
(if no grammar
errors)
Machine Code
Compiler program
for L (in M1)
same
Compiler program
for Java (in M2)
produces
produces
Program P
in machine
language of JVM
JVM in M1
Machine M1
same
Program P
in machine
language of JVM
JVM in M2
Program P in
language L
Compiler program
for L (in M2)
produces
Program P
in machine
language of M1
Program P
in machine
language of M2
Machine M1
Machine M2
machine
dependable
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Self Check 1.6
What is the code for the Java virtual machine instruction "Load
the contents of memory location 100"?
Program P in
language Java
Compiler program
for Java (in M1)
same
Machine
dependable
Can be
executed in
any JVM
Machine
dependable
Machine M2
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Self Check 1.6
Self Check 1.7
What is the code for the Java virtual machine instruction "Load
the contents of memory location 100"?
Does a person who uses a computer for office work ever run a
compiler?
Answer: 21 100
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
5
8/18/2011
Self Check 1.7
The Java Programming Language
Does a person who uses a computer for office work ever run a
compiler?
• Simple
Answer: No – a compiler is intended for programmers, to
translate high-level programming instructions into machine
code.
• Safe
• Platform-independent ("write once, run anywhere")
• Rich library (packages)
• Designed for the internet
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Applets on a Web Page
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Java Versions
Version
Year
1.0
1996
Important New Features
1.1
1997
Inner classes
1.2
1998
Swing, Collections
1.3
2000
Performance enhancements
1.4
2002
Assertions, XML
5
2004
Generic classes, enhanced for loop, auto-boxing, enumerations
6
2006
Library improvements
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Self Check 1.8
Self Check 1.8
What are the two most important benefits of the Java language?
What are the two most important benefits of the Java language?
Answer: Safety and portability.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
6
8/18/2011
Self Check 1.9
Self Check 1.9
How long does it take to learn the entire Java library?
How long does it take to learn the entire Java library?
Answer: No one person can learn the entire library – it is too
large.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Becoming Familiar with your Computer
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
A Shell Window
• Log in
• Locate the Java compiler
• Understand files and folders
• Programs are kept in files
• File: a collection of items of information that are kept together
• Files have names, and the rules for legal names differ from one system to
another
• Files are stored in folders or directories; these file containers can be
nested
• Write a simple program (later)
• Save your work
• Develop a strategy for keeping backup copies of your work
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
An Integrated Development Environment
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Nested Folders
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
7
8/18/2011
Self Check 1.10
Self Check 1.10
How are programming projects stored on a computer?
How are programming projects stored on a computer?
Answer: Programs are stored in files, and files are stored in
folders or directories.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Self Check 1.11
Self Check 1.11
What do you do to protect yourself from data loss when you work
on programming projects?
What do you do to protect yourself from data loss when you work
on programming projects?
Answer: You back up your files and folders.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
HelloPrinter
ch01/hello/HelloPrinter.java
in a Console Window
1: public class HelloPrinter
2: {
3:
public static void main(String[] args)
4:
{
5:
// Display a greeting in the console window
6:
7:
System.out.println("Hello, World!");
8:
}
9: }
Output:
Hello, World!
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
8
8/18/2011
HelloPrinter
in an IDE
A Simple Program
• public class ClassName
• public static void main(String[] args)
• // comment
• Method call
System class
System.out object
println method
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Syntax 1.1 Method Call
Self Check 1.12
object.methodName(parameters)
How would you modify the HelloPrinter program to print the
words "Hello," and "World!" on two lines?
Example:
System.out.println("Hello, Dave!");
Purpose:
To invoke a method of an object and supply any additional
parameters.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Self Check 1.12
Self Check 1.13
How would you modify the HelloPrinter program to print the
words "Hello," and "World!" on two lines?
Would the program continue to work if you omitted the line starting
with //?
Answer:
System.out.println("Hello,");
System.out.println("World!");
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
9
8/18/2011
Self Check 1.13
Self Check 1.14
Would the program continue to work if you omitted the line starting
with //?
What does the following set of statements print?
System.out.print("My lucky number is");
System.out.println(3 + 4 + 5);
Answer: Yes – the line starting with // is a comment, intended
for human readers. The compiler ignores comments.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Self Check 1.14
Errors
What does the following set of statements print?
• Syntax errors
System.out.print("My lucky number is");
System.out.println(3 + 4 + 5);
Answer: The printout is
System.ouch.print(". . .");
System.out.print("Hello);
• Detected by the compiler
My lucky number is12
• Logic errors
It would be a good idea to add a space after the is.
System.out.print("Hell");
• Detected (hopefully) through testing
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Self Check 1.15
Self Check 1.15
Suppose you omit the // characters from the HelloPrinter.java
program but not the remainder of the comment. Will you get a
compile-time error or a run-time error?
Suppose you omit the // characters from the HelloPrinter.java
program but not the remainder of the comment. Will you get a
compile-time error or a run-time error?
Answer: A compile-time error. The compiler will not know what
to do with the word Display.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
10
8/18/2011
Self Check 1.16
Self Check 1.16
How can you find logic errors in a program?
How can you find logic errors in a program?
Answer: You need to run the program and observe its behavior.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
The Compilation Process
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
The Edit-Compile-Test Loop
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Self Check 1.17
Self Check 1.17
What do you expect to see when you load a class file into your
text editor?
What do you expect to see when you load a class file into your
text editor?
Answer: A sequence of random characters, some funnylooking. Class files contain virtual machine instructions that are
encoded as binary numbers.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
11
8/18/2011
Self Check 1.18
Self Check 1.18
Why can't you test a program for run-time errors when it has
compiler errors?
Why can't you test a program for run-time errors when it has
compiler errors?
Answer: When a program has compiler errors, no class file is
produced, and there is nothing to run.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
12