liang, introduction to java programming,revised by dai-kaiyu 1 chapter 1 introduction to computers,...

78
Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java Chapter1 Introduction to Com puters, Program s, and Java Chapter2 Prim itive D ata Typesand O perations Chapter3 ControlStatem ents Chapter5 A rrays Chapter4 M ethods Basic com puterskillssuch asusing W indow s, InternetExplorer, and M icrosoftW ord Prerequisitesfor PartI

Upload: tyler-garrett

Post on 25-Dec-2015

234 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu1

Chapter 1 Introduction to Computers, Programs, and Java

Chapter 1 Introduction to Computers, Programs, and Java

Chapter 2 Primitive Data Types and Operations

Chapter 3 Control Statements

Chapter 5 Arrays

Chapter 4 Methods

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word

Prerequisites for Part I

Page 2: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu2

Objectives To review computer basics, programs, and operating systems

(§1.2-1.4). To represent numbers in binary, decimal, and hexadecimal (§1.5

Optional). 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).

Page 3: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu3

What Is a Computer?

Computer Performs computations and makes logical decisions Millions / billions times faster than human beings

Computer programs Sets of instructions for which computer processes data

Hardware Physical devices of computer system

Software Programs that run on computers

Page 4: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu4

Computer Organization Six logical units of computer system

Central processing unit (CPU) Supervises operation of other devices Two components:

• Control Unit• Arithmetic and logic unit (ALU)

Memory unit RAM(Ramdom-access memory)

storage Devices Hard drives, floppy drives

Input and output Devices Input unit

Mouse, keyboard Output unit

Printer, monitor, audio speakers

Page 5: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu5

What is a Computer?

A computer consists of a CPU, memory, hard disk, floppy disk, monitor, printer, and communication devices.

Memory

CPU

Storage Devices

Disk, CD, and Tape

Input Devices

Output Devices

Keyboard, Mouse

Monitor, Printer

Communication Devices

Modem, and NIC

Page 6: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu6

CPUThe central processing unit (CPU) is the brain of a computer

retrieves instructions from memory and executes them.

measured in megahertz (MHz), with 1 megahertz equaling 1 million pulses per second. 1 gigahertz is 1000 megahertz

Page 7: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu7

MemoryMemory is to store data and program instructions for CPU to execute. A program and its data must be brought to memory before they can be executed.

A memory unit is an ordered sequence of bytes, each holds eight bits.

Page 8: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu8

How Data is Stored?Computers use zeros and ones The programmers need not to be concerned about the encoding and decoding of databyte is the minimum storage unit.Every byte has a unique addressRAM(radom-access memory)

.

.

.

2000

2001

2002

2003

2004

.

.

.

01001010

01100001

01110110

01100001

00000011

Memory content

Memory address

Encoding for character ‘J’ Encoding for character ‘a’ Encoding for character ‘v’ Encoding for character ‘a’ Encoding for number 3

Page 9: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu9

Storage DevicesInformation in memory is lost when the power is off

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

Storage Devices

Disk, CD, and Tape

Input Devices

Output Devices

Keyboard, Mouse

Monitor, Printer

Communication Devices

Modem, and NIC

Page 10: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu10

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

The resolution specifies the number of pixels per square inch. The higher the resolution, the sharper and clearer the image is.

The dot pitch is the amount of space between pixels. The smaller the dot pitch, the better the display.

Memory

CPU

Storage Devices

Disk, CD, and Tape

Input Devices

Output Devices

Keyboard, Mouse

Monitor, Printer

Communication Devices

Modem, and NIC

Page 11: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu11

Communication Devicesregular modem : 56,000 bps (bits per second).

DSL (digital subscriber line) also uses a phone line and can transfer data in a speed 20 times faster than a regular modem.

cable modem uses the TV cable line

Network interface card (NIC) is a device to connect a computer to a local area network (LAN). 10BaseT, can transfer data at 10 mbps (million bits per second).

Memory

CPU

Storage Devices

Disk, CD, and Tape

Input Devices

Output Devices

Keyboard, Mouse

Monitor, Printer

Communication Devices

Modem, and NIC

Page 12: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu12

Programming LanguagesMachine Language Assembly Language High-Level Language

Machine language is a set of primitive instructions built into every computer. The instructions are in the form of binary code

“Natural language” of computer component Machine dependent

For example, to add two numbers: 

1101101010011010

Page 13: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu13

Programming LanguagesMachine Language Assembly Language High-Level Language

Assembly languages are English-like abbreviations represent computer operationsa program called assembler is used to convert assembly language programs into machine code. to add two numbers : ADDF3 R1, R2, R3

… ADDF3 R1, R2, R3 …

Assembly Source File

Assembler

… 1101101010011010 …

Machine Code File

Page 14: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu14

Programming LanguagesMachine Language Assembly Language High-Level Language

The high-level languages are English-like and easy to learn and program.

area = 5 * 5 * 3.1415;

Page 15: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu15

Popular High-Level Languages

Java (We use it in the book)COBOL (COmmon Business Oriented Language)FORTRAN

FORmula TRANslation By IBM 1954-1957

BASIC Beginner All-purpose Symbolic Instructional Code 1965, Dartmouth university

Pascal (named for Blaise Pascal) designed for teaching structured programming

Ada (named for Ada Lovelace)

Page 16: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu16

Popular High-Level Languages

C (whose developer designed B first) Visual Basic (Basic-like visual language developed

by Microsoft) Delphi (Pascal-like visual language developed by

Borland) C++ (an object-oriented language, based on C)

Page 17: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu17

Compiling Source Code

Compiler Compiler help to convert to machine language

Interpreter Execute high-level language programs without

compilation

Compiler Source File Object File Linker Excutable File

Page 18: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu18

Operating Systems

The operating system (OS) is a program that manages and controls a computer’s activities.Controlling and monitoring system activitiesAllocating and assigning system resourcesScheduling operations

Multiprogramming Multithreading multiprocessing

User

Application Programs

Operating System

Hardware

Unix Windows Linux

Page 19: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu19

Number Systems

0, 1

0, 1, 2, 3, 4, 5, 6, 7

0, 1, 2, 3, 4, 5, 6, 7, 8, 9

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F

binary

octal

decimal

hexdecimal

Page 20: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu20

Binary Numbers => Decimals

Given a binary numberthe equivalent decimal value is

10 in binary = 2 in decimal

1000 in binary = 8 in decimal

01221 ... bbbbbb nnn

01221 222...222 01221 bbbbbb nnn

nnn

021 1

0202021 23

10101011 in binary

= 171 in decimal

121202120212021 234567

Page 21: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu21

Decimals => Binary To convert a decimal number d to a binary number is to find the binary digits.. such that

These numbers can be found by successively dividing d by 2 until the quotient is 0. The remainders are

For example, the decimal number 123 is 1111011 in binary. The conversion is conducted as follows:

01221 ,,,...,,, bbbbbb nnn

01221 222...222 01221 bbbbbbd nnn

nnn

01221 ,,,...,,, bbbbbb nnn

123 2

61

122

1

b0

61 2

30

60

30 2

15

30

15 2

7

14

7 2

3

6

3 2

1

2

1 2

0

0

1

b1

0

b2

1

b3

1

b4

1

b5

1

b6

Remainder

Quotient

Page 22: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu22

Windows Calculator

Page 23: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu23

Hexadecimals => Decimals

The hexadecimal number system has sixteen digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F. Given a hexadecimal number

The equivalent decimal value is 01221 161616...161616 01221

hhhhhh nnnnnn

7F in hex 15167 1 = 127 in decimal

FFFF in hex = 65535 in decimal15161516151615 23

01221 ... hhhhhh nnn

Page 24: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu24

Decimals => Hexadecimal

To convert a decimal number d to a hexadecimal number is to find the hexadecimal digits such that

01221 161616...161616 01221 hhhhhhd nnn

nnn

123 16

7

112

11

h0

7

0

0

7

h1

16

01221 ,,,...,,, hhhhhh nnn

Page 25: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu25

Hexadecimal Binary

0000 0 00001 1 10010 2 20011 3 30100 4 40101 5 50110 6 60111 7 71000 8 8 1001 9 91010 A 101011 B 111100 C 121101 D 131110 E 141111 F 15

Binary Hex DecimalTo convert a hexadecimal number to a binary number, simply convert each digit in the

hexadecimal number into a four-digit binary number.

To convert a binary number to a hexadecimal, convert every four binary digits from left to right in the binary number into a hexadecimal number. For example,

1 1 1 0 0 0 1 1 0 1

D 8 3

Page 26: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu26

Brief introduction to java

Why JavaObject-orientedStrong fuctionsWidely used in industryJava is the Internet programming language Java is a general purpose programming

language

Page 27: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu27

History of JavaJava

Originally for intelligent consumer-electronic devices

Oak, Java Then used for creating Web pages with dynamic content,

announced at a major conference in May 1995 Now also used for:

Develop large-scale enterprise applications(J2EE) Enhance WWW server functionality Provide applications for consumer devices (cell phones,

etc.)

Page 28: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu28

Java 的历史与现状

Java 之父 James Gosling 1983 年获得了美国卡耐基梅隆大学的计算机学博士学位,毕业

后曾经效力于 IBM ,后转而加盟 Sun 。 1990 年, Sun 公司开始组建团队研究“绿色计划”, James

Gosling 开始负责为设备和用户之间的交流创建一种能够实现网络交互的语言。

1995 年 5 月, Sun 公司正式发布 Java 编程语言及平台。                                     

补充了解

Page 29: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu29

Java 的历史与现状 Java 的应用现状(“ 2005 Java One 开发者大

会”) 历史上从没有过像 Java 这样的平台能够如此广泛地被应用,目

前全球 Java 经济价值已经超过 1,200 亿美元 目前全球运行 Java 的设备已达到 25 亿, Java 开发人员超过 45

0 万,基于 Java 技术的智能卡达 10 亿,基于 Java 技术的手机达 7.08 亿,采用 Java 技术的 PC 机达 7 亿, JCP(Java Community Process , Java 社区进程 ) 成员达 912 个,运营商们部署 Java 平台 140 多个。

Sun 认为,数字媒体将是 Java 的下一个重点市场,同时,教育和健康将是未来 Java 发展过程中的两大重点应用领域

信息时代正在成为历史,一个参与时代已经来临

---sun

补充了解

Page 30: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu30

今天的 JavaReadex 调查公司最近所做的一份开发人员调查显示, C/C ++ 和 Java 在开发语言使用比例上的优势已经牢不可破,而且未来期望一项, Java 占据了极大比例, C/C++ 以及 VB 都在明显下降。

C++ 专家 ThomasPlum 也做过一个有趣的实验,使用正则表达式筛选网上招聘中的编程语言要求,结果 Java 以 43% 左右的比例仅略低于老资格的 C++ ,排名第二,远超过了 C 、 VB 、 .NET 类语言。

《 SoftwareDevelopment 》杂志最新的读者调查结果来看, Java 和 J2EE等相关技术人员的薪水已经稳稳高出同侪

在学界,海外的大专院校几乎都已经清一色地以 Java 为默认教学语言,数据结构、操作系统乃至编译、图形学等等科目通通采用 Java 实现。连美国院校委员会( CollegeBoard )针对高中生的 AP 考试也用 Java 替换了原来的 C

补充了解

美国排名前 15 大学程序设计调查报告

Page 31: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu31

JVM

• Java programs are executed within a program called the JVM..

Page 32: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu32

Java 语言的特点"write once, run anywhere. "

                                                                                                                                                                                    

补充了解

Page 33: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu33

Java Platform补充了解

Page 34: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu34

Desktop on Java ?补充了解

Page 35: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu35

Personal, Distributed and Client/Server Computing

Personal computing Computers for personal use (1977Apple,1981IBM

Ltd.)Distributed computing

Computing performed among several computersClient/server computing

Servers offer common store of programs and data (File server )

Clients access programs and data from server

补充了解

Page 36: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu36

Two tier C/S system补充了解

Page 37: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu37

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

Optional

Page 38: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu38

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

Java is partially modeled on C++, but greatly simplified and improved. Some people refer to Java as "C++--"

Page 39: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu39

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

Java is inherently object-oriented. Object-oriented programming

Structured programming

Regards the individuls in world as objects. Each object has its own feature and act as its manners. Different objects can communicate with each other, thus let the world run

Take the problem needed to be resolved as the core point and use computer logic to discribe the problem and the method to resolve it.

Page 40: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu40

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

networking capability is inherently integrated into Java

Page 41: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu41

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

The bytecode is machine-independent and can run on any machine that has a Java interpreter, which is part of the Java Virtual Machine (JVM).

Page 42: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu42

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

Java compilers can detect many problems that would first show up at execution time in other languages.

Java has eliminated certain types of error-prone programming constructs found in other languages.

Java has a runtime exception-handling feature to provide programming support for robustness.

Page 43: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu43

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

Java implements several security mechanisms to protect your system against harm caused by stray programs.

Page 44: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu44

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

Write once, run anywhere

With a Java Virtual Machine (JVM), you can write one program that will run on any platform.

Page 45: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu45

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

Because Java is architecture neutral, Java programs are portable. They can be run on any platform without being recompiled.

Page 46: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu46

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

JIT technology: bytecodenative code

HotSpot Performance engineoptimizing the frequently used code

Page 47: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu47

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

Multithread programming is smoothly integrated in Java, whereas in other languages you have to call procedures specific to the operating system to enable multithreading.

Page 48: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu48

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

Java was designed to adapt to an evolving environment. New code can be loaded on the fly without recompilation. There is no need for developers to create, and for users to install, major new software versions. New features can be incorporated transparently as needed.

Page 49: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu49

JDK Versions

JDK 1.02 (1995)JDK 1.1 (1996)

major changesJava 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)

Jdk 5.0 tiger Jdk 6.0 mustang Jdk 7.0 dorphin

Page 50: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu50

JDK Editions

Java Standard Edition (J2SE) J2SE can be used to develop client-side standalone applications or

applets.

Java Enterprise Edition (J2EE) J2EE can be used to develop server-side applications such as Java

servlets and Java ServerPages.

Java Micro Edition (J2ME). J2ME can be used to develop applications for mobile devices such

as cell phones.

This book uses J2SE to introduce Java programming.

Page 51: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu51

Java IDE Tools

Borland JBuilderNetBeans Open Source by Sun Sun ONE Studio by Sun MicroSystems

Eclipse Open Source by IBM

Show case Our focus is on the language, editplus, Jcreator is preferred

Page 52: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu52

A Simple Java Program

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

RunRun

SourceSource

Example 1.1

Page 53: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu53

A Simple Program: Printing a Line of Text Sample program

Show program, then analyze each line

1 // Welcome1.java2 // A first program in Java.3 4 public class Welcome1 { 5 6 // main method begins execution of Java application7 public static void main( String args[] )8 {9 System.out.println( "Welcome to Java Programming!" );10 11 } // end method main12 13 } // end class Welcome1

Welcome to Java Programming!

Page 54: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu54

A Simple Program: Printing a Line of Text

Comments start with: // Comments ignored during program execution Document and describe code Provides code readability

Multiple line comments: /* ... *//* This is a multiple

line comment. It can be split over many lines */

Another line of comments Note: line numbers not part of program, added for reference

1 Welcome1.java

2 // A first program in Java.

Page 55: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu55

Blank line Makes program more readable Blank lines, spaces, and tabs are white-space characters

• Ignored by compiler

Begins class definition for class Welcome1 Every Java program has at least one user-defined class Keyword: words reserved for use by Java

• class keyword followed by class name Naming classes: capitalize every word

• SampleClassName

A Simple Program: Printing a Line of Text

3

4 public class Welcome1 {

Page 56: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu56

A Simple Program: Printing a Line of Text

Name of class called identifier Series of characters consisting of letters, digits,

underscores ( _ ) and dollar signs ( $ ) Does not begin with a digit, has no spaces Examples: Welcome1, $value, _value, button7

• 7button is invalid Java is case sensitive (capitalization matters)

• a1 and A1 are different use public keyword

Certain details not important now Mimic certain features, discussions later

4 public class Welcome1 {

Page 57: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu57

A Simple Program: Printing a Line of Text

Saving files File name must be class name with .java extension Welcome1.java For public class, the file name must be identical to the class

name Left brace {

Begins body of every class Right brace ends definition (line 13)

Part of every Java application Applications begin executing at main

• Parenthesis indicate main is a method• Java applications contain one or more methods

4 public class Welcome1 {

7 public static void main( String args[] )

Page 58: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu58

A Simple Program: Printing a Line of Text

Exactly one method must be called main Methods can perform tasks and return information

void means main returns no information For now, mimic main's first line

Left brace begins body of method definition Ended by right brace } (line 11)

7 public static void main( String args[] )

8 {

Page 59: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu59

A Simple Program: Printing a Line of Text

Instructs computer to perform an action Prints string of characters

• String - series characters inside double quotes White-spaces in strings are not ignored by compiler

System.out Standard output object Print to command window (i.e., MS-DOS prompt)

9 System.out.println( "Welcome to Java Programming!" );

Page 60: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu60

A Simple Program: Printing a Line of Text

Method System.out.println Displays line of text Argument inside parenthesis Position the output cursor to the beginning of the next line in

the command window This line known as a statement

Statements must end with semicolon ; or will make a syntax error(violations of the language rules)

9 System.out.println( "Welcome to Java Programming!" );

Page 61: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu61

A Simple Program: Printing a Line of Text

9 System.out.println( "Welcome to Java Programming!" );

Page 62: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu62

A Simple Program: Printing a Line of Text

Ends method definition

Ends class definition Can add comments to keep track of ending braces Remember, compiler ignores comments Comments can start on same line after code

11 } // end method main

13 } // end class Welcome1

Page 63: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu63

Creating and Editing Using NotePad

To use NotePad, type notepad Welcome.java

from the DOS prompt.

Page 64: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu64

Creating, Compiling, and Running Programs

Source Code

Create/Modify Source Code

Compile Source Code i.e., javac Welcome.java

Bytecode

Run Byteode i.e., java Welcome

Result

If compilation errors

If runtime errors or incorrect result

package chapter1; public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }

… Method Welcome() 0 aload_0 … Method void main(java.lang.String[]) 0 getstatic #2 … 3 ldc #3 <String "Welcome to Java!"> 5 invokevirtual #4 … 8 return

Saved on the disk

stored on the disk

Source code (developed by the programmer)

Byte code (generated by the compiler for JVM to read and interpret, not for you to understand)

Page 65: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu65

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

Page 66: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu66

Compiling and Running Java from Editplus

TextPadOptional

Page 67: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu67

Compiling and Running Java from JCreator

Page 68: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu68

Compiling and Running Java from JBuilder

JBuilderOptional

Page 69: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu69

Compiling and Running Java from NetBeans

NetBeansOptional

Page 70: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu70

Anatomy of a Java Program

Comments two slashes (//) in a line enclosed between /* and */ in one or multiple lines. between /** and * / : javadoc comments

Package Act as a directory to group classes Group of all packages known as Java class library or Java

applications programming interface (Java API) Core package and extension package

Reserved words Key words, case-sensitive

Page 71: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu71

Anatomy of a Java Program

ModifiersStatementsBlocksClassesMethodsThe main method

Page 72: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu72

Blocks

A pair of braces in a program forms a block that groups

components of a program.

public class Test { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }

Class block

Method block

Page 73: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu73

main MethodThe 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;

}

Page 74: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu74

Displaying Text in a Message Dialog Box

you can use the showMessageDialog method in the JOptionPane class. JOptionPane is one of the many predefined classes in the Java system, which can be reused rather than “reinventing the wheel.”

RunRun

SourceSource

Page 75: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu75

The showMessageDialog Method

JOptionPane.showMessageDialog(null, "Welcome to Java!", "Example 1.2 Output", JOptionPane.INFORMATION_MESSAGE));

Page 76: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu76

Two Ways to Invoke the Method

use the showMessageDialog method use a statement as shown in the example:

JOptionPane.showMessageDialog(null, x,

y, JOptionPane.INFORMATION_MESSAGE));

x is a string for the text to be displayed,

y is a string for the title of the message dialog box.

use a statement like this:JOptionPane.showMessageDialog(null, x);

where x is a string for the text to be displayed.

Page 77: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu77

caution

System.out.println(2/3);

JOptionPane.showMessageDialog(null, 2/3);

JOptionPane.showMessageDialog(null, "" + (double)2/3);

System.out.println vs. JOptionPane.showMessageDialog(null, String);

Syntax error

Page 78: Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 1 Introduction to Computers, Programs, and Java

Liang, Introduction to Java Programming,revised by Dai-kaiyu78

The exit Method

Prior to JDK 1.5, you have to invoke System.exit() to terminate the program if the program uses JOptionPane dialog boxes. Since JDK 1.5, it is not necessary.

Calls static method exit of class SystemTerminates application

•Use with any application displaying a GUIBecause method is static, needs class name and dot (.)Identifiers starting with capital letters usually class names

Argument of 0 means application ended successfullyNon-zero usually means an error occurredThe value is passed to the command window that executed the program.

Class System part of package java.langNo import statement neededjava.lang automatically imported in every Java program

补充了解