Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Chapter One: Introduction
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
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
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Prerequisites
• Computer savvy (file management, text editing)
• Problem solving skills
• Time management
• High school basic math
• No prior programming background required
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
What Is Programming?
• Computers are programmed to perform tasks
• Different tasks = different programs
• Program
• 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.
The Anatomy of a Computer
• 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.
The Anatomy of a Computer
• 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.
bj4_01_04
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
• The first usable electronic computer
• designed by John Mauchly and J. Presper Eckert of the
University of Pennsylvania
• Completed by 1946
• Contained about 18000 vaccuum tubes in many cabinates
• Weighed more than 27 tons was roughly 2.4 m × 0.9 m × 30 m
• Consumed 150 kW of power
• Vaccuum tubes burned out at the rate of several tubes per day
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
The ENIAC
• Work on the ENIAC was supported by U.S. Navy
•Computation of ballistic table give the trajectory of a projectile
•Depend on
• the wind resistance
• initial velocity
• atmospheric condition
• 1950’s the word computer referred to people who did that kind of
work
• the ENIAC was later used for peaceful purposes such as
tabulator of U.S. census data
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.
Translating Human-Readable programs to
Machine Code
• the processor executes machine instructions
• CPU from different vendors have different sets of machine
instructions.
• Java programs contain machine instructions for a so-called
“Java virtual machine” (JVM) that being enable Java
applications to run on different CPUs without modification
• Java virtual machine is an idealized CPU that is simulated by a
program run on the actual CPU
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Machine Code
• Java Virtual Machine (JVM) – a typical sequence of machine
instructions is:
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.
• Machine instructions are encoded as numbers:
21 40
16 100
163 240
• Compiler translates high-level language to machine code
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Java Virtual Machine (JVM)
Java Virtual Machine
• fetch sequence of numbers
21 40
16 100
163 240
• decode them
• execute the associated sequence of commands
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Communicate the command sequence to the computer
• The most direct method is to place the actual numbers into
computer memory
• It is tedious and error-prone to look up the numeric codes for
thousands command and manually place the code into
memory.
• Computer are good at automating tedious and error-prone
activities.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
High-level Programming Language
• Began to appear in the mid-1950s
• Programmer expresses the idea behind the task that needs to
be performed
• Compiler translates the high-level description into machine
instructions for a particular processor
For example
Java instruction
If (intRate > 100)
System.out.println(“Interest rate error”);
Translate into
21 40 16 100 163 240 …
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
The Java Programming Language
• Originaly named Oak
• Developed as a part of the Green project at the Sun
Microsystems for use in consumer devices in 1991.
• Designed by James Gosling, Patrick Naughton and others in
1994.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
The Java Programming Language
• Simple
• 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
Visit web page
that contains Java code,
the code automaticlly running.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Java Versions
Version
Year
Important New Features
1.0
1996
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
10
2010
Small language changes and library improvments
Java was revised and extended many times
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Java library
• You cannot hope to learn all of Java in one term
• Java itself is relative simple but Java has a vast library
with support for
–
–
–
–
–
–
–
Graphics
User interface design
Cryptography
Networking
Sound
Database storage
Many other purposes
• Expert Java programmers just use some parts that they
need to particular projects
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.
ch01/hello/HelloPrinter.java
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.
HelloPrinter in an IDE
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
HelloPrinter.java
HelloPrinter.java
Start a new class
1: public class HelloPrinter
2: {
3:
public static void main(String[] args)
4:
{
comment
5:
// Display a greeting in the console window
6:
7:
System.out.println("Hello, World!");
8:
}
Output:
9: }
Hello, World!
•Classes are a fundamental concept in Java.
•In Java, every program consists of one or more classes
•The word “public” denotes that
the class is usable by the public
•In Java, every source file can contain at one public class
and the name of the public class must match
the name of the file containing the class
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
main method
public static void main(String[] args)
{
….
}
•The above construction declares a method called main
•A method contains a collection of programming instructions
•Every Java application must have a main method.
•Most Java programs contain other methods beside main.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
ch01/hello/HelloPrinter.java
public static void main(String[] args)
{
….
}
•The above construction declares a method called main
•A method contains a collection of programming instructions
•Every Java application must have a main method.
•Most Java programs contain other methods beside main.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
HelloPrinter.java
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: }
Start a new class
Body of
main
method
•Statement inside the curly brackets are executed one by one.
•Each statement ends in a semicolon(;)
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
System.out
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: }
•The console window is represented in Java by an object called System.out.
•An object is an entity that you manipulate in your program
•In Java, each object belongs to a class
•The class declares methods that specify what you can do with the objects
•System.out object belongs to PrintStream class
•PrintStream class has a method called println for printing a line of text.
called Syste.out
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
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.
Syntax 1.1 Method Call
object.methodName(parameters)
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.
bj4_syn_01_01
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Self Check
How would you modify the HelloPrinter program to print the
words "Hello," and "World!" on two lines?
Answer:
System.out.println("Hello,");
System.out.println("World!");
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Self Check 1.13
Would the program continue to work if you omitted the line starting
with //?
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.
Self Check 1.14
What does the following set of statements print?
System.out.print("My lucky number is");
System.out.println(3 + 4 + 5);
Answer: The printout is
My lucky number is12
It would be a good idea to add a space after the is.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Errors
• Syntax errors
System.ouch.print(". . .");
System.out.print("Hello);
• Detected by the compiler
• Logic errors
System.out.print("Hell");
• Detected (hopefully) through testing
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
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?
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.
The Compilation Process
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.
program
bytecodes class files
API
class files
bytecodes
Class loader
Execution
engine
Java virtual machine
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.
Source files and Class files
• The source file of a Java class is stored in a file:
ClassName.java
Java source files are essentially text files and can be opened in
any text editor
• The compiled file of a Java class is stored in a file:
ClassName.class
Java class files are binary files (not human-readable).
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.
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.
Homework
• Obtain a Course Book
• Install Java and Eclipse
• Eclipse IDE:
– Read the following eclipse tutorials
– Help -> Help contents ->
•
Workbench User Guide
– Basic tutorial thru Deleting Resources
•
Java Development User Guide
– Basic tutorial thru Running Your Program
» Don’t worry about understanding details of JUnit sample
project
Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.