01 Slide
01 Slide
Chapter 1 Introduction to
Computers, Programs, and Java
2
Objectives
• To review computer basics, programs, and
operating systems (§1.2-1.4).
• To understand the relationship between Java and
the World Wide Web (§1.6).
• To know Java’s advantages (§1.7).
• To distinguish the terms API, IDE, and JDK (§1.8).
• To write a simple Java program (§1.9).
• To create, compile, and run Java programs (§1.10).
• To understand the Java runtime environment
(§1.10).
• To know the basic syntax of a Java program (§1.11).
• To display output on the console and on the dialog
box (§1.12).
3
What is a Computer?
A computer consists of a CPU, memory, hard disk, floppy disk,
monitor, printer, and communication devices.
Memory
CPU
Modem, and Communication Output Monitor,
NIC Devices Devices Printer
4
CPU
The central processing unit (CPU) is the brain of a computer. It
retrieves instructions from memory and executes them. The CPU
speed is measured in megahertz (MHz), with 1 megahertz equaling 1
million pulses per second. The speed of the CPU has been improved
continuously. If you buy a PC now, you can get an Intel Pentium 4
Processor at 3 gigahertz (1 gigahertz is 1000 megahertz).
Memory
CPU
Modem, and Communication Output Monitor,
NIC Devices Devices Printer
5
Memory
Memory is to store data and program instructions for CPU to
execute. A memory unit is an ordered sequence of bytes, each holds
eight bits. A program and its data must be brought to memory before
they can be executed. A memory byte is never empty, but its initial
content may be meaningless to your program. The current content of
a memory byte is lost whenever new information is placed in it.
Memory
CPU
Modem, and Communication Output Monitor,
NIC Devices Devices Printer
6
Storage Devices
Memory is volatile, because information is lost when the power is
off. Programs and data are permanently stored on storage devices
and are moved to memory when the computer actually uses them.
There are three main types of storage devices:Disk drives (hard
disks and floppy disks), CD drives (CD-R and CD-RW), and Tape
drives.
Memory
CPU
Modem, and Communication Output Monitor,
NIC Devices Devices Printer
8
Memory
CPU
Modem, and Communication Output Monitor,
NIC Devices Devices Printer
9
Programs
Computer programs, known as software, are
instructions to the computer.
Machine language
primitive instructions built into every computer
the only language the computer understands!
varies across platforms
form: binary code
11
Programming Languages
Machine Language Assembly Language High-Level Language
High-level languages
English-like
Easy to learn and program
Must be translated to machine code before execution
Compilation
Vs.
Interpretation
12
Java Byte-Code
Output of Java compiler: byte-code, not executable code.
Running Byte-Code:
Java Virtual Machine (JVM) – a software simulation of a CPU whose
machine language is byte-code. Thus, byte-code enables the JVM to
execute faster than interpreters typically do.
Just In Time (JIT) compiler – compiles Java byte-code into
machine code before execution.
Java chip – Java byte-code is the machine code of the
device. Java Bytecode
Why Java?
The answer is that Java enables users to deploy
applications on the Internet for servers, desktop computers,
and small hand-held devices. The future of computing will
be profoundly influenced by the Internet, and Java
promises to remain a big part of that future.
Java’s History
• James Gosling and Sun Microsystems
• Oak
• Java, May 20, 1995, Sun World
• HotJava
▫ The first Java-enabled Web browser
• Early History Website:
http://java.sun.com/features/1998/05/birthday.html
16
Optional
Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
• Java Is Interpreted
• Java Is Robust
• Java Is Secure
• Java Is Architecture-Neutral
• Java Is Portable
• Java's Performance
• Java Is Multithreaded
• Java Is Dynamic
17
Characteristics of Java
Java Is Simple Java is partially modeled on C++, but greatly
Java Is Object- simplified and improved. Some people refer to
Java as "C++--" because it is like C++ but
Oriented with more functionality and fewer negative
Java Is Distributed aspects.
Java Is Interpreted
Java Is Robust
Java Is Secure
Java Is Architecture-
Neutral
Java Is Portable
Java's Performance
Java Is Multithreaded
Java Is Dynamic
18
Characteristics of Java
Java Is Simple Java is inherently object-oriented.
Java Is Object- Although many object-oriented languages
began strictly as procedural languages,
Oriented Java was designed from the start to be
Java Is Distributed object-oriented. Object-oriented
Java Is Interpreted programming (OOP) is a popular
programming approach that is replacing
Java Is Robust
traditional procedural programming
Java Is Secure techniques.
Java Is Architecture-
One of the central issues in software
Neutral development is how to reuse code. Object-
Java Is Portable oriented programming provides great
Java's Performance flexibility, modularity, clarity, and
Java Is Multithreaded reusability through encapsulation,
inheritance, and polymorphism.
Java Is Dynamic
19
Characteristics of Java
Java Is Simple Distributed computing involves several
Java Is Object- computers working together on a network.
Java is designed to make distributed
Oriented computing easy. Since networking
Java Is Distributed capability is inherently integrated into
Java Is Interpreted Java, writing network programs is like
sending and receiving data to and from a
Java Is Robust
file.
Java Is Secure
Java Is Architecture-
Neutral
Java Is Portable
Java's Performance
Java Is Multithreaded
Java Is Dynamic
20
Characteristics of Java
Java Is Simple You need an interpreter to run Java
Java Is Object- programs. The programs are compiled into
the Java Virtual Machine code called
Oriented bytecode. The bytecode is machine-
Java Is Distributed independent and can run on any machine
Java Is Interpreted that has a Java interpreter, which is part of
the Java Virtual Machine (JVM).
Java Is Robust
Java Is Secure
Java Is Architecture-
Neutral
Java Is Portable
Java's Performance
Java Is Multithreaded
Java Is Dynamic
21
Characteristics of Java
Java Is Simple Java compilers can detect many problems
Java Is Object- that would first show up at execution time
in other languages.
Oriented
Java Is Distributed Java has eliminated certain types of error-
Java Is Interpreted prone programming constructs found in
other languages.
Java Is Robust
Java Is Secure Java has a runtime exception-handling
Java Is Architecture- feature to provide programming support
for robustness.
Neutral
Java Is Portable
Java's Performance
Java Is Multithreaded
Java Is Dynamic
22
Characteristics of Java
Java Is Simple
Java Is Object-
Oriented
Java Is Distributed
Java Is InterpretedJava implements several security
Java Is Robust mechanisms to protect your system against
harm caused by stray programs.
Java Is Secure
Java Is Architecture-
Neutral
Java Is Portable
Java's Performance
Java Is Multithreaded
Java Is Dynamic
23
Characteristics of Java
Java Is Simple
Java Is Object-
Oriented
Java Is Distributed
Java Is Interpreted
Java Is Robust
Java Is Secure
Java Is Architecture- Write once, run anywhere
JDK Versions
• JDK 1.02 (1995)
• JDK 1.1 (1996)
major changes
• Java 2 SDK v 1.2 (a.k.a JDK 1.2, 1998)
• Java 2 SDK v 1.3 (a.k.a JDK 1.3, 2000)
• Java 2 SDK v 1.4 (a.k.a JDK 1.4, 2002)
• Java 2 SDK v 1.5 (a.k.a JDK 1.5, 2004)
29
JDK Editions
Java Development Kit is the software
distribution containing tools for
developing and running Java applications
• Java Standard Edition (J2SE)
▫ Desktop applications or applets.
▫ We will be using!
• Java Enterprise Edition (J2EE)
▫ Server-side applications (i.e., Java servlets and
Java ServerPages).
• Java Micro Edition (J2ME)
▫ Applications for mobile devices such as cell
phones .
30
JRE
JRE – Java Runtime Environment
Software and classes for running Java
application s
Code Development
The most primitive (and simplest)
development environment under Windows:
use Notepad and the Command Prompt.
www.prenhall.com/liang
Direct link at
www.cs.armstrong.edu/liang/
intro5e.html
35
Compiling and Running Java from
the Command Window
Set path to JDK bin directory
set path=c:\Program Files\java\
jdk1.5.0\bin
Set classpath to include the
current directory
set classpath=.
Compile
javac Welcome.java
Run
java Welcome
36
Java Comments
Line comments: following // in one line
Javadoc comment
between /** and */
To document a class or method
can be extracted into HTML file using javadoc
command
38
Reserved Words
Reserved words or keywords are words that
have a specific meaning to the compiler and
cannot be used for other purposes in the
program. For example, when the compiler sees
the word class, it understands that the word
after class is the name for the class. Other
reserved words in Example 1.1 are public,
static, and void. Their use will be introduced
later in the book.
39
Statements
A statement represents an action or a sequence
of actions. The statement
System.out.println("Welcome to Java!") in the
program in Example 1.1 is a statement to
display the greeting "Welcome to Java!" Every
statement in Java ends with a semicolon (;).
40
Blocks
A pair of braces in a program forms a block that groups
components of a program.
Classes
The class is the essential Java construct. A class
is a template or blueprint for objects. To
program in Java, you must understand classes
and be able to write and use them. The mystery
of the class will continue to be unveiled
throughout this book. For now, though,
understand that a program is defined by using
one or more classes.
42
Methods
What is System.out.println? It is a method: a
collection of statements that performs a
sequence of operations to display a message on
the console. It can be used even without fully
understanding the details of how it works. It is
used by invoking a statement with a string
argument. The string argument is enclosed
within parentheses. In this case, the argument
is "Welcome to Java!" You can call the same
println method with a different argument to
print a different message.
43
main Method
The main method provides the control of
program flow. The Java interpreter executes the
application by invoking the main method.
Applet
• Runs on client's machine from within a
Web page
• Compilation process is the same as for an
application
javac HelloWorldApplet.java
45
import javax.swing.JApplet;
import java.awt.Graphics;
Deploying an Applet
Add applet tag to body of html file
<applet code=”HelloWorldApplet.class” width=”350”
height=”300”></applet>
View the Web page
Open html file in browser
appletviewer utility that comes with the JDK
appletviewer
HelloWorldApplet.html
47
Applet - Differences
No main method
Paint method (function) is called when the applet
needs to be displayed
extends keyword means that our applet is inherited
from the Applet class
(more about inheritance later!)