0% found this document useful (0 votes)
3 views47 pages

01 Slide

Uploaded by

Stricker Man
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views47 pages

01 Slide

Uploaded by

Stricker Man
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 47

1

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

Disk, CD, and Storage Input Keyboard,


Tape Devices Devices Mouse

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

Disk, CD, and Storage Input Keyboard,


Tape Devices Devices Mouse

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

Disk, CD, and Storage Input Keyboard,


Tape Devices Devices Mouse

CPU
Modem, and Communication Output Monitor,
NIC Devices Devices Printer
6

How Data is Stored?


Data of various kinds, such as
numbers, characters, and strings,
are encoded as a series of bits (zeros
and ones). Computers use zeros and
ones because digital devices have Memory address Memory content
two stable states, which are referred
to as zero and one by convention. . .
The programmers need not to be . .
concerned about the encoding and . .
decoding of data, which is performed 2000 01001010 Encoding for character ‘J’
automatically by the system based 2001 01100001 Encoding for character ‘a’
on the encoding scheme. The 2002 01110110 Encoding for character ‘v’
encoding scheme varies. For 2003 01100001
example, character ‘J’ is represented 2004 00000011
Encoding for character ‘a’

by 01001010 in one byte. A small Encoding for number 3

number such as three can be stored


in a single byte. If computer needs to
store a large number that cannot fit
into a single byte, it uses a number
of adjacent bytes. No two data can
share or split a same byte. A byte is
the minimum storage unit.
7

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

Disk, CD, and Storage Input Keyboard,


Tape Devices Devices Mouse

CPU
Modem, and Communication Output Monitor,
NIC Devices Devices Printer
8

Output Devices: Monitor


The monitor displays information (text and graphics). The resolution
and dot pitch determine the quality of the display.

Memory

Disk, CD, and Storage Input Keyboard,


Tape Devices Devices Mouse

CPU
Modem, and Communication Output Monitor,
NIC Devices Devices Printer
9

Programs
Computer programs, known as software, are
instructions to the computer.

You tell a computer what to do through programs.


Without programs, a computer is an empty
machine. Computers do not understand human
languages, so you need to use computer
languages to communicate with them.

Programs are written using programming


languages.
10
Programming Languages
Machine Language Assembly Language High-Level Language

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

 Advantages Java Virtual


Machine
High degree of platform independence Any
Fast introduction to new platforms Computer
13

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 is a general purpose programming language.


Java is the Internet programming language.
14

Java, Web, and Beyond


Java can be used to develop
•Java desktop applications.
•Java Applets
•Java Servlets and JavaServer Pages
•applications for hand-held devices
(i.e., Palm, cell phones)
15

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

Neutral With a Java Virtual Machine (JVM),


 Java Is Portable you can write one program that
 Java's Performance will run on any platform.
 Java Is Multithreaded
 Java Is Dynamic
24
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 Because Java is architecture
neutral, Java programs are
 Java Is Portable
portable. They can be run on any
 Java's Performance platform without being
 Java Is Multithreadedrecompiled.
 Java Is Dynamic
25
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’s performance Because Java
is architecture neutral, Java
 Java Is Portable
programs are portable. They can
 Java's Performance be run on any platform without
 Java Is Multithreadedbeing recompiled.
 Java Is Dynamic
26
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
Multithread programming is smoothly
 Java's Performance integrated in Java, whereas in other
 Java Is Multithreaded
languages you have to call
 Java Is Dynamic procedures specific to the operating
system to enable multithreading.
27
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 was designed to adapt to an
evolving environment. New code can be
 Java's Performance loaded on the fly without recompilation.
 Java Is Multithreaded
There is no need for developers to create,
and for users to install, major new
 Java Is Dynamic software versions. New features can be
incorporated transparently as needed.
28

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

API vs. JDK vs. JRE


31

Code Development
The most primitive (and simplest)
development environment under Windows:
use Notepad and the Command Prompt.

 Edit your code using Notepad


 Compile (using javac) to create a class file
 Javac myProgram.java
 Execute (using java)
 Java myProgram
32

Java IDE Tools


• Borland JBuilder
• NetBeans Open Source by Sun
• Sun ONE Studio by Sun MicroSystems
• Eclipse Open Source by IBM
33

A Simple Java Program


Example 1.1
//This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
34

Supplements on the Companion


Website
 See Supplement A for installing
and configuring JDK 1.5
 See Supplement B for compiling
and running Java from the
command window for details

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

Anatomy of a Java Program


• Comments
• Reserved words
• Modifiers
• Statements
• Blocks
• Classes
• Methods
• The main method
37

Java Comments
 Line comments: following // in one line

Block comments: between /* and */

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.

public class Test {


Class block
public static void main(String[] args) {
System.out.println("Welcome to Java!"); Method block
}
}
41

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.

The main method looks like this:

public static void main(String[] args) {


// Statements;
}
44

Applet
• Runs on client's machine from within a
Web page
• Compilation process is the same as for an
application
javac HelloWorldApplet.java
45

A Simple Java Applet


//this applet prints “Hello World” in a rectangle

import javax.swing.JApplet;
import java.awt.Graphics;

public class HelloWorld extends JApplet {


public void paint(Graphics g) {
g.drawRect(0, 0,
getSize().width - 1,
getSize().height - 1);
g.drawString("Hello world!", 5, 15);
}
}
46

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!)

You might also like