1、Liang,Introduction to Java Programming,Eighth Edition,(c)2011 Pearson Education,Inc.All rights reserved.01321308071Chapter 1 Introduction to Computers,Programs,and JavaLiang,Introduction to Java Programming,Eighth Edition,(c)2011 Pearson Education,Inc.All rights reserved.01321308072ObjectivesFTo rev
2、iew computer basics,programs,and operating systems(1.2-1.4).FTo explore the relationship between Java and the World Wide Web(1.5).FTo distinguish the terms API,IDE,and JDK(1.6).FTo write a simple Java program(1.7).FTo display output on the console(1.7).FTo explain the basic syntax of a Java program(
3、1.7).FTo create,compile,and run Java programs(1.8).F(GUI)To display output using the JOptionPane output dialog boxes(1.9).Liang,Introduction to Java Programming,Eighth Edition,(c)2011 Pearson Education,Inc.All rights reserved.01321308073What is a Computer?A computer consists of a CPU,memory,hard dis
4、k,floppy disk,monitor,printer,and communication devices.CPU e.g.,Disk,CD,and Tape Input Devices e.g.,Keyboard,Mouse e.g.,Monitor,Printer Communication Devices e.g.,Modem,and NIC Storage Devices Memory Output Devices Bus Liang,Introduction to Java Programming,Eighth Edition,(c)2011 Pearson Education,
5、Inc.All rights reserved.01321308074CPUThe 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 continuous
6、ly.If you buy a PC now,you can get an Intel Pentium 4 Processor at 3 gigahertz(1 gigahertz is 1000 megahertz).Liang,Introduction to Java Programming,Eighth Edition,(c)2011 Pearson Education,Inc.All rights reserved.01321308075MemoryMemory is to store data and program instructions for CPU to execute.A
7、 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 informatio
8、n is placed in it.Liang,Introduction to Java Programming,Eighth Edition,(c)2011 Pearson Education,Inc.All rights reserved.01321308076How 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 di
9、gital devices have 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 automatically by the system based on the encoding scheme.The encoding scheme varies.For example,character J is
10、represented by 01001010 in one byte.A small 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.2000 2001 2002
11、 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 Liang,Introduction to Java Programming,Eighth Edition,(c)2011 Pearson Education,Inc.All rig
12、hts reserved.01321308077Storage DevicesMemory 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 an
13、d floppy disks),CD drives(CD-R and CD-RW),and Tape drives.Liang,Introduction to Java Programming,Eighth Edition,(c)2011 Pearson Education,Inc.All rights reserved.01321308078Output Devices:MonitorThe monitor displays information(text and graphics).The resolution and dot pitch determine the quality of
14、 the display.Liang,Introduction to Java Programming,Eighth Edition,(c)2011 Pearson Education,Inc.All rights reserved.01321308079Monitor Resolution and Dot PitchThe resolution specifies the number of pixels per square inch.Pixels(short for“picture elements”)are tiny dots that form an image on the scr
15、een.The resolution can be set manually.The higher the resolution,the sharper and clearer the image is.However,the image may be very small if you set high resolution on a small screen monitor.PC monitors are usually 15-inch,17-inch,19-inch,or 21-inch.For a 15-inch monitor,a comfortable resolution set
16、ting would be 640480(307,200 pixels).resolutionThe dot pitch is the amount of space between pixels.The smaller the dot pitch,the better the display.dot pitchLiang,Introduction to Java Programming,Eighth Edition,(c)2011 Pearson Education,Inc.All rights reserved.013213080710Communication DevicesA regu
17、lar modem uses a phone line and can transfer data in a speed up to 56,000 bps(bits per second).A DSL(digital subscriber line)also uses a phone line and can transfer data in a speed 20 times faster than a regular modem.A cable modem uses the TV cable line maintained by the cable company.A cable modem
18、 is as fast as a DSL.Network interface card(NIC)is a device to connect a computer to a local area network(LAN).The LAN is commonly used in business,universities,and government organizations.A typical type of NIC,called 10BaseT,can transfer data at 10 mbps(million bits per second).Liang,Introduction
19、to Java Programming,Eighth Edition,(c)2011 Pearson Education,Inc.All rights reserved.013213080711ProgramsComputer 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 hu
20、man languages,so you need to use computer languages to communicate with them.Programs are written using programming languages.Liang,Introduction to Java Programming,Eighth Edition,(c)2011 Pearson Education,Inc.All rights reserved.013213080712Programming LanguagesMachine Language Assembly Language Hi
21、gh-Level LanguageMachine language is a set of primitive instructions built into every computer.The instructions are in the form of binary code,so you have to enter binary codes for various instructions.Program with native machine language is a tedious process.Moreover the programs are highly difficu
22、lt to read and modify.For example,to add two numbers,you might write an instruction in binary like this:1101101010011010Liang,Introduction to Java Programming,Eighth Edition,(c)2011 Pearson Education,Inc.All rights reserved.013213080713Programming LanguagesMachine Language Assembly Language High-Lev
23、el LanguageAssembly languages were developed to make programming easy.Since the computer cannot understand assembly language,however,a program called assembler is used to convert assembly language programs into machine code.For example,to add two numbers,you might write an instruction in assembly co
24、de like this:ADDF3 R1,R2,R3 ADDF3 R1,R2,R3 Assembly Source File Assembler 1101101010011010 Machine Code File Liang,Introduction to Java Programming,Eighth Edition,(c)2011 Pearson Education,Inc.All rights reserved.013213080714Programming LanguagesMachine Language Assembly Language High-Level Language
25、The high-level languages are English-like and easy to learn and program.For example,the following is a high-level language statement that computes the area of a circle with radius 5:area=5*5*3.1415;Liang,Introduction to Java Programming,Eighth Edition,(c)2011 Pearson Education,Inc.All rights reserve
26、d.013213080715Popular High-Level LanguagesFCOBOL(COmmon Business Oriented Language)FFORTRAN(FORmula TRANslation)FBASIC(Beginner All-purpose Symbolic Instructional Code)FPascal(named for Blaise Pascal)FAda(named for Ada Lovelace)FC(whose developer designed B first)FVisual Basic(Basic-like visual lang
27、uage developed by Microsoft)FDelphi(Pascal-like visual language developed by Borland)FC+(an object-oriented language,based on C)FC#(a Java-like language developed by Microsoft)FJava(We use it in the book)Liang,Introduction to Java Programming,Eighth Edition,(c)2011 Pearson Education,Inc.All rights r
28、eserved.013213080716Compiling Source CodeA program written in a high-level language is called a source program.Since a computer cannot understand a source program.Program called a compiler is used to translate the source program into a machine language program called an object program.The object pro
29、gram is often then linked with other supporting library code before the object can be executed on the machine.Compiler Source File Machine-language File Linker Executable File Library Code Liang,Introduction to Java Programming,Eighth Edition,(c)2011 Pearson Education,Inc.All rights reserved.0132130
30、80717Operating SystemsThe operating system(OS)is a program that manages and controls a computers activities.You are probably using Windows 98,NT,2000,XP,or ME.Windows is currently the most popular PC operating system.Application programs such as an Internet browser and a word processor cannot run wi
31、thout an operating system.User Application Programs Operating System Hardware Liang,Introduction to Java Programming,Eighth Edition,(c)2011 Pearson Education,Inc.All rights reserved.013213080718Why Java?The answer is that Java enables users to develop and deploy applications on the Internet for serv
32、ers,desktop computers,and small hand-held devices.The future of computing is being profoundly influenced by the Internet,and Java promises to remain a big part of that future.Java is the Internet programming language.FJava is a general purpose programming language.FJava is the Internet programming l
33、anguage.Liang,Introduction to Java Programming,Eighth Edition,(c)2011 Pearson Education,Inc.All rights reserved.013213080719Java,Web,and BeyondFJava can be used to develop Web applications.FJava AppletsFJava Web ApplicationsFJava can also be used to develop applications for hand-held devices such as
34、 Palm and cell phonesLiang,Introduction to Java Programming,Eighth Edition,(c)2011 Pearson Education,Inc.All rights reserved.013213080720Examples of Javas Versatility(Applets)Liang,Introduction to Java Programming,Eighth Edition,(c)2011 Pearson Education,Inc.All rights reserved.013213080721PDA and C
35、ell PhoneLiang,Introduction to Java Programming,Eighth Edition,(c)2011 Pearson Education,Inc.All rights reserved.013213080722Javas HistoryFJames Gosling and Sun MicrosystemsFOakFJava,May 20,1995,Sun WorldFHotJava The first Java-enabled Web browserFEarly History Website:http:/ to Java Programming,Eig
36、hth Edition,(c)2011 Pearson Education,Inc.All rights reserved.013213080723Characteristics of JavaFJava Is Simple FJava Is Object-Oriented FJava Is Distributed FJava Is Interpreted FJava Is Robust FJava Is Secure FJava Is Architecture-Neutral FJava Is Portable FJavas Performance FJava Is Multithreade
37、d FJava Is Dynamic Companion Websitewww.cs.armstrong.edu/liang/intro8e/JavaCharacteristics.pdfLiang,Introduction to Java Programming,Eighth Edition,(c)2011 Pearson Education,Inc.All rights reserved.013213080724Characteristics of JavaFJava Is Simple FJava Is Object-Oriented FJava Is Distributed FJava
38、 Is Interpreted FJava Is Robust FJava Is Secure FJava Is Architecture-Neutral FJava Is Portable FJavas Performance FJava Is Multithreaded FJava Is Dynamic Java is partially modeled on C+,but greatly simplified and improved.Some people refer to Java as C+-because it is like C+but with more functional
39、ity and fewer negative aspects.Companion WebsiteLiang,Introduction to Java Programming,Eighth Edition,(c)2011 Pearson Education,Inc.All rights reserved.013213080725Characteristics of JavaFJava Is Simple FJava Is Object-Oriented FJava Is Distributed FJava Is Interpreted FJava Is Robust FJava Is Secur
40、e FJava Is Architecture-Neutral FJava Is Portable FJavas Performance FJava Is Multithreaded FJava Is Dynamic Java is inherently object-oriented.Although many object-oriented languages began strictly as procedural languages,Java was designed from the start to be object-oriented.Object-oriented progra
41、mming(OOP)is a popular programming approach that is replacing traditional procedural programming techniques.One of the central issues in software development is how to reuse code.Object-oriented programming provides great flexibility,modularity,clarity,and reusability through encapsulation,inheritan
42、ce,and polymorphism.Companion WebsiteLiang,Introduction to Java Programming,Eighth Edition,(c)2011 Pearson Education,Inc.All rights reserved.013213080726Characteristics of JavaFJava Is Simple FJava Is Object-Oriented FJava Is Distributed FJava Is Interpreted FJava Is Robust FJava Is Secure FJava Is
43、Architecture-Neutral FJava Is Portable FJavas Performance FJava Is Multithreaded FJava Is Dynamic Distributed computing involves several computers working together on a network.Java is designed to make distributed computing easy.Since networking capability is inherently integrated into Java,writing
44、network programs is like sending and receiving data to and from a file.Companion WebsiteLiang,Introduction to Java Programming,Eighth Edition,(c)2011 Pearson Education,Inc.All rights reserved.013213080727Characteristics of JavaFJava Is Simple FJava Is Object-Oriented FJava Is Distributed FJava Is In
45、terpreted FJava Is Robust FJava Is Secure FJava Is Architecture-Neutral FJava Is Portable FJavas Performance FJava Is Multithreaded FJava Is Dynamic You need an interpreter to run Java programs.The programs are compiled into the Java Virtual Machine code called bytecode.The bytecode is machine-indep
46、endent and can run on any machine that has a Java interpreter,which is part of the Java Virtual Machine(JVM).Companion WebsiteLiang,Introduction to Java Programming,Eighth Edition,(c)2011 Pearson Education,Inc.All rights reserved.013213080728Characteristics of JavaFJava Is Simple FJava Is Object-Ori
47、ented FJava Is Distributed FJava Is Interpreted FJava Is Robust FJava Is Secure FJava Is Architecture-Neutral FJava Is Portable FJavas Performance FJava Is Multithreaded FJava Is Dynamic Java compilers can detect many problems that would first show up at execution time in other languages.Java has el
48、iminated certain types of error-prone programming constructs found in other languages.Java has a runtime exception-handling feature to provide programming support for robustness.Companion WebsiteLiang,Introduction to Java Programming,Eighth Edition,(c)2011 Pearson Education,Inc.All rights reserved.0
49、13213080729Characteristics of JavaFJava Is Simple FJava Is Object-Oriented FJava Is Distributed FJava Is Interpreted FJava Is Robust FJava Is Secure FJava Is Architecture-Neutral FJava Is Portable FJavas Performance FJava Is Multithreaded FJava Is Dynamic Java implements several security mechanisms
50、to protect your system against harm caused by stray programs.Companion WebsiteLiang,Introduction to Java Programming,Eighth Edition,(c)2011 Pearson Education,Inc.All rights reserved.013213080730Characteristics of JavaFJava Is Simple FJava Is Object-Oriented FJava Is Distributed FJava Is Interpreted