You are on page 1of 74

Chapter 1 Introduction to Computers, Programs, and Java

Prerequisites for Part I
Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word

Chapter 1 Introduction to Computers, Programs, and Java Chapter 2 Primitive Data Types and Operations Chapter 3 Control Statements Chapter 4 Methods

Chapter 5 Arrays
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

1

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).
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

2

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

Memory

Disk, CD, and Tape

Storage Devices CPU

Input Devices

Keyboard, Mouse

Modem, and NIC

Communication Devices

Output Devices

Monitor, Printer

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

3

All rights reserved. Inc. The speed of the CPU has been improved continuously. Memory Disk. The CPU speed is measured in megahertz (MHz). and NIC Communication Devices Output Devices Monitor. you can get an Intel Pentium 4 Processor at 3 gigahertz (1 gigahertz is 1000 megahertz).CPU The central processing unit (CPU) is the brain of a computer. Printer Liang. Fifth Edition. Mouse Modem. If you buy a PC now. and Tape Storage Devices CPU Input Devices Keyboard. CD. with 1 megahertz equaling 1 million pulses per second. It retrieves instructions from memory and executes them. (c) 2005 Pearson Education. 0-13-148952-6 4 . Introduction to Java Programming.

Memory Memory is to store data and program instructions for CPU to execute. 0-13-148952-6 5 . Printer Liang. Introduction to Java Programming. Memory Disk. Mouse Modem. and Tape Storage Devices CPU Input Devices Keyboard. 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. (c) 2005 Pearson Education. A program and its data must be brought to memory before they can be executed. A memory byte is never empty. and NIC Communication Devices Output Devices Monitor. A memory unit is an ordered sequence of bytes. Fifth Edition. CD. All rights reserved. Inc. each holds eight bits.

Computers use zeros and ones because digital devices have two stable states. No two data can share or split a same byte. . For example. and strings. such as numbers. are encoded as a series of bits (zeros and ones). Inc. A byte is the minimum storage unit. character „J‟ is represented by 01001010 in one byte. . The encoding scheme varies. If computer needs to store a large number that cannot fit into a single byte. . it uses a number of adjacent bytes. All rights reserved. . characters. which is performed automatically by the system based on the encoding scheme. Introduction to Java Programming. which are referred to as zero and one by convention. Fifth Edition. A small number such as three can be stored in a single byte.How Data is Stored? Data of various kinds. The programmers need not to be concerned about the encoding and decoding of data. (c) 2005 Pearson Education. 01001010 01100001 01110110 01100001 00000011 Encoding for character „J‟ Encoding for character „a‟ Encoding for character „v‟ Encoding for character „a‟ Encoding for number 3 Liang. 2000 2001 2002 2003 2004 . Memory address Memory content . 0-13-148952-6 6 .

Printer Liang. Inc. Programs and data are permanently stored on storage devices and are moved to memory when the computer actually uses them. Mouse Modem. 0-13-148952-6 7 . All rights reserved. CD drives (CD-R and CD-RW). Fifth Edition. (c) 2005 Pearson Education. and Tape drives. Memory Disk. because information is lost when the power is off.Storage Devices Memory is volatile. CD. and NIC Communication Devices Output Devices Monitor. Introduction to Java Programming. and Tape Storage Devices CPU Input Devices Keyboard. There are three main types of storage devices:Disk drives (hard disks and floppy disks).

CD. All rights reserved. Printer Liang. Inc. 0-13-148952-6 8 . and Tape Storage Devices CPU Input Devices Keyboard. Memory Disk. Introduction to Java Programming. Fifth Edition. The resolution and dot pitch determine the quality of the display. and NIC Communication Devices Output Devices Monitor. (c) 2005 Pearson Education.Output Devices: Monitor The monitor displays information (text and graphics). Mouse Modem.

or 21-inch. Pixels (short for “picture elements”) are tiny dots that form an image on the screen. 17-inch. the image may be very small if you set high resolution on a small screen monitor. dot pitch The dot pitch is the amount of space between pixels. Introduction to Java Programming. a comfortable resolution setting would be 640480 (307. Fifth Edition.Monitor Resolution and Dot Pitch resolution The resolution specifies the number of pixels per square inch. the sharper and clearer the image is. (c) 2005 Pearson Education.200 pixels). The higher the resolution. The smaller the dot pitch. All rights reserved. However. the better the display. 19-inch. Inc. For a 15inch monitor. PC monitors are usually 15-inch. The resolution can be set manually. Liang. 0-13-148952-6 9 .

Mouse Modem. Network interface card (NIC) is a device to connect a computer to a local area network (LAN). (c) 2005 Pearson Education. Introduction to Java Programming. Printer 10 Liang. A typical type of NIC. and NIC Communication Devices Output Devices Monitor. universities. can transfer data at 10 mbps (million bits per second). Memory Disk. and government organizations. and Tape Storage Devices CPU Input Devices Keyboard. Inc. called 10BaseT. 0-13-148952-6 .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.Communication Devices A regular modem uses a phone line and can transfer data in a speed up to 56. A cable modem uses the TV cable line maintained by the cable company. The LAN is commonly used in business. A cable modem is as fast as a DSL. CD. All rights reserved. Fifth Edition.

Introduction to Java Programming. Liang. Computers do not understand human languages. Fifth Edition. (c) 2005 Pearson Education. Without programs. You tell a computer what to do through programs. known as software. are instructions to the computer. Programs are written using programming languages. so you need to use computer languages to communicate with them.Programs Computer programs. a computer is an empty machine. Inc. All rights reserved. 0-13-148952-6 11 .

For example. Program with native machine language is a tedious process. 0-13-148952-6 12 . All rights reserved. Introduction to Java Programming. Inc. so you have to enter binary codes for various instructions.Programming Languages Machine Language Assembly Language High-Level Language Machine language is a set of primitive instructions built into every computer. Moreover the programs are highly difficult to read and modify. to add two numbers. Fifth Edition. The instructions are in the form of binary code. (c) 2005 Pearson Education. you might write the an instruction in binary like this: 1101101010011010 Liang.

0-13-148952-6 13 . (c) 2005 Pearson Education. Inc. R3 … Machine Code File Assembler … 1101101010011010 … Liang. R3 Assembly Source File … ADDF3 R1. to add two numbers. you might write an instruction in assembly code like this: ADDF3 R1. Fifth Edition. Since the computer cannot understand assembly language. For example. R2.Programming Languages Machine Language Assembly Language High-Level Language Assembly languages were developed to make programming easy. a program called assembler is used to convert assembly language programs into machine code. however. All rights reserved. Introduction to Java Programming. R2.

All rights reserved. (c) 2005 Pearson Education. Fifth Edition. Liang.Programming Languages Machine Language Assembly Language High-Level Language The high-level languages are English-like and easy to learn and program. 0-13-148952-6 14 .1415. the following is a high-level language statement that computes the area of a circle with radius 5: area = 5 * 5 * 3. For example. Introduction to Java Programming. Inc.

0-13-148952-6 15 . Introduction to Java Programming. (c) 2005 Pearson Education. Inc.Popular High-Level Languages Java (We use it in the book) COBOL (COmmon Business Oriented Language) FORTRAN (FORmula TRANslation) BASIC (Beginner All-purpose Symbolic Instructional Code) Pascal (named for Blaise Pascal) Ada (named for Ada Lovelace) 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. Fifth Edition. based on C) Liang. All rights reserved.

Source File Compiler Object File Linker Excutable File Liang. Inc. The object program is often then linked with other supporting library code before the object can be executed on the machine. Program called a compiler is used to translate the source program into a machine language program called an object program. Introduction to Java Programming. Since a computer cannot understand a source program. Fifth Edition.Compiling Source Code A program written in a high-level language is called a source program. 0-13-148952-6 16 . All rights reserved. (c) 2005 Pearson Education.

0-13-148952-6 17 . The source program must be recompiled. however. and compile the source program into a special type of object code. Java Bytecode Java Virtual Machine Any Computer Liang. Introduction to Java Programming. Java was designed to run object programs on any platform.5.Compiling Source Code You can port a source program to any machine with appropriate compilers. you write the program once. Inc. as shown in Figure 1. known as bytecode. With Java. The bytecode can then run on any computer with a Java Virtual Machine. because the object program can only run on a specific machine. Fifth Edition. All rights reserved. Nowadays computers are networked to work together. (c) 2005 Pearson Education. Java Virtual Machine is a software that interprets Java bytecode.

Operating Systems The operating system (OS) is a program that manages and controls a computer‟s activities. 0-13-148952-6 18 . All rights reserved. Fifth Edition. or ME. XP. NT. Application programs such as an Internet browser and a word processor cannot run without an operating system. User Application Programs Operating System Hardware Liang. Inc. 2000. (c) 2005 Pearson Education. Windows is currently the most popular PC operating system. Introduction to Java Programming. You are probably using Windows 98.

Number Systems NOTE: You can skip this section and use it as reference when you have questions regarding binary and hexadecimal numbers. 9 0. Fifth Edition. 2. 5. 0-13-148952-6 19 . A. D. (c) 2005 Pearson Education. 6. 6. 8. 6. 7. 1 0. 3. 5. 1. Inc. 7. All rights reserved. 1. 2. 2. 7 decimal hexdecimal 0. 9. binary octal 0. 3. 3. 8. 4. E. 5. Introduction to Java Programming. B. F Liang. 4. 1. 4. C.

computer software is used to convert decimal numbers into binary numbers. 7. Binary numbers are not intuitive. Liang. and 9. The binary number system has two digits. short for binary digit. and vice versa. Fifth Edition. it is assumed to be a decimal number. 0-13-148952-6 20 . 1. 2. The digits in the decimal number system are 0. 8. Each 0 and 1 is called a bit. 5. Internally. Introduction to Java Programming. 4. Computers use binary numbers internally because storage devices like memory and disk are made to store 0s and 1s. since we use decimal numbers in our daily life. All rights reserved. 3. When you write a number like 20 in a program.Number Systems NOTE: You can skip this section and use it as reference when you have questions regarding binary and hexadecimal numbers. 6. (c) 2005 Pearson Education. A number or a text inside a computer is stored as a sequence of 0s and 1s. Inc. 0 and 1.

3. Liang. The letters A. Fifth Edition. 11. B. D. 2. and F. C. 12. 8. Binary numbers tend to be very long and cumbersome. C. and F correspond to the decimal numbers 10. 9. 6. 5. 13. 7. Hexadecimal numbers are often used to abbreviate binary numbers. cont. 1. 4. D. E. 0-13-148952-6 21 . 14. The hexadecimal number system has 16 digits: 0. E. (c) 2005 Pearson Education.Number Systems. B. Inc. A. and 15. Introduction to Java Programming. All rights reserved.

. (c) 2005 Pearson Education. 0-13-148952-6 .Binary Numbers => Decimals Given a binary number bnbn  1bn  2..b2b1b0 the equivalent decimal value is bn  2n  bn  1  2n1  bn  2  2n2  ... Introduction to Java Programming. All rights reserved. Inc. Fifth Edition.  b2  22  b1  21  b0  20 10 in binary 1000 in binary 10101011 in binary 1  21  0 = 2 in decimal = 8 in decimal = 171 in decimal 22 1  23  0  2 2  0  2  0 1  2 7  0  2 6  1  25  0  2 4  1  23  0  2 2  1  2  1 Liang.

(c) 2005 Pearson Education. bn  1..b2. Inc.. b0 For example..b2. bn  2.. The remainders are bn.  b2  2 2  b1  21  b0  20 These numbers can be found by successively dividing d by 2 until the quotient is 0. Introduction to Java Programming... All rights reserved. b1.Decimals => Binary To convert a decimal number d to a binary number is to find the binary digits. Fifth Edition. b0 such that d  bn  2n  bn  1  2n1  bn  2  2n2  . bn  1. 0-13-148952-6 23 . bn  2. b1.... bn. The conversion is conducted as follows: Quotient 2 61 123 122 Remainder 1 b0 2 30 61 60 1 b1 2 15 30 30 0 b2 2 7 15 14 1 b3 2 3 7 6 1 b4 2 1 3 2 1 b5 2 0 1 0 1 b6 Liang.. the decimal number 123 is 1111011 in binary..

Windows Calculator The Windows Calculator is a useful tool for performing number conversions. Fifth Edition. and Calculator from the Start button. Introduction to Java Programming. All rights reserved. choose Programs. 0-13-148952-6 24 . Liang. Accessories. (c) 2005 Pearson Education. To run it. Inc.

Fifth Edition. C.h2h1h0 a hexadecimal number The equivalent decimal value is hn 16 n  hn  1 16 n1  hn  2 16 n2  . C.. 3. B. D. A. Given hnhn  1hn  2.. and 15. 9. E. 14. 7. The letters A. (c) 2005 Pearson Education. 4. 0-13-148952-6 25 . Introduction to Java Programming. 11. 6. 8. 1. 13. 12. 5. Inc. D. and F correspond to the decimal numbers 10.  h2 16 2  h1 161  h0 16 0 7F in hex 7  16  15 1 = 127 in decimal FFFF in hex 15  16 3  15  16 2  15  16  15 = 65535 in decimal Liang. and F. B. E. 2. All rights reserved.Hexadecimals => Decimals The hexadecimal number system has sixteen digits: 0...

h1. the decimal number 123 is 7B in hexadecimal.Decimals => Hexadecimal To convert a decimal number d to a hexadecimal number is to find the hexadecimal digits hn.. Introduction to Java Programming.  2... such that hn.. h1.hn  2... Inc. . The conversion is conducted as follows: 16 123 112 16 7 0 11 7 h0 h1 Liang.. hn-1. (c) 2005 Pearson Education. The remainders are h0. h0 d  hn  16 n  hn  1  16 n 1  hn  2  16 n  2  .. hn hn-2.. h2. All rights reserved.. h n  1. hn For example. 0-13-148952-6 26 .  h2  16 2  h1  161  h0  16 0 7 0 These numbers can be found by successively dividing d by 16 until the quotient is 0.h 2. hn  1... Fifth Edition.

1110001101 3 8 D Liang. 0-13-148952-6 27 . (c) 2005 Pearson Education.Hexadecimal  Binary Binary Hex Decimal 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 0 1 2 3 4 5 6 7 8 9 A B C D E F 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 To convert a hexadecimal number to a binary number. simply convert each digit in the hexadecimal number into a four-digit binary number. All rights reserved. Inc. 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. Fifth Edition. For example. Introduction to Java Programming.

desktop computers. The future of computing will be profoundly influenced by the Internet. is the Internet programming language.Why Java? The answer is that Java enables users to deploy applications on the Internet for servers. All rights reserved. Fifth Edition. and Java promises to remain a big part of that future. Liang. Inc. Java Java is a general purpose programming language. Introduction to Java Programming. and small hand-held devices. 0-13-148952-6 28 . (c) 2005 Pearson Education.

Fifth Edition. 0-13-148952-6 29 . (c) 2005 Pearson Education.Examples of Java‟s Versatility Standalone Application: TicTacToe Applet: TicTacToe SelfTest Web site Computing: Cell phones Servlets: Mobile Liang. Introduction to Java Programming. Inc. All rights reserved.

(c) 2005 Pearson Education. Inc.  Java Applets  Java Servlets and JavaServer Pages  Java can also be used to develop applications for hand-held devices such as Palm and cell phones Liang.Java. 0-13-148952-6 30 . Fifth Edition. All rights reserved. and Beyond  Java can be used to develop Web applications. Introduction to Java Programming. Web.

Fifth Edition.sun. All rights reserved. Gosling and Sun Microsystems May 20. 0-13-148952-6 31 . Introduction to Java Programming. (c) 2005 Pearson Education. Inc. 1995. Sun World  HotJava – The first Java-enabled Web browser  Early History Website: http://java.html Liang.Java‟s History  James  Oak  Java.com/features/1998/05/birthday.

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  Liang. 0-13-148952-6 32 . All rights reserved. Fifth Edition. Introduction to Java Programming. (c) 2005 Pearson Education. Inc.

Introduction to Java Programming. All rights reserved. but greatly Java Is Simple simplified and improved. 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 Liang. Inc.Characteristics of Java            Java is partially modeled on C++. Fifth Edition. (c) 2005 Pearson Education. Some people refer to Java Is Object-Oriented Java as "C++--" because it is like C++ but with more functionality and fewer negative Java Is Distributed aspects. 0-13-148952-6 33 .

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. Although many object-oriented languages began strictly as procedural languages, Java was designed from the start to be object-oriented. Object-oriented programming (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. Objectoriented programming provides great flexibility, modularity, clarity, and reusability through encapsulation, inheritance, and polymorphism.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

34

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

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 network programs is like sending and receiving data to and from a file.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

35

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

You need an interpreter to run Java programs. The programs are compiled into the Java Virtual Machine code called bytecode. The bytecode is machineindependent and can run on any machine that has a Java interpreter, which is part of the Java Virtual Machine (JVM).

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

36

Introduction to Java Programming.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. Liang. Java has a runtime exception-handling feature to provide programming support for robustness. Inc. Fifth Edition. 0-13-148952-6 37 . (c) 2005 Pearson Education. Java has eliminated certain types of errorprone programming constructs found in other languages. All rights reserved.

Java Is Secure Java Is Architecture-Neutral Java Is Portable Java's Performance Java Is Multithreaded Java Is Dynamic Liang. Fifth Edition.Characteristics of Java            Java Is Simple Java Is Object-Oriented Java Is Distributed Java Is Interpreted Java implements several security Java Is Robust mechanisms to protect your system against harm caused by stray programs. Introduction to Java Programming. 0-13-148952-6 38 . Inc. (c) 2005 Pearson Education. All rights reserved.

Introduction to Java Programming. All rights reserved. Liang.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. Fifth Edition. run anywhere With a Java Virtual Machine (JVM). Inc. 0-13-148952-6 39 . (c) 2005 Pearson Education. you can write one program that will run on any platform.

Java Is Dynamic Liang.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. Fifth Edition. (c) 2005 Pearson Education. They can Java's Performance be run on any platform without being Java Is Multithreaded recompiled. Java Is Portable Java programs are portable. All rights reserved. Inc. 0-13-148952-6 40 . Introduction to Java Programming.

Java Is Dynamic Liang.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 Java Is Portable architecture neutral. 0-13-148952-6 41 . They can be run on any Java Is Multithreaded platform without being recompiled. Fifth Edition. (c) 2005 Pearson Education. Inc. Introduction to Java Programming. All rights reserved. Java programs are Java's Performance portable.

Introduction to Java Programming. All rights reserved. (c) 2005 Pearson Education. Inc.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 Multithread programming is smoothly Java Is Multithreaded integrated in Java. whereas in other languages you have to call procedures Java Is Dynamic Liang. 42 . Fifth Edition. 0-13-148952-6 specific to the operating system to enable multithreading.

New code can be loaded on the Java Is Multithreaded fly without recompilation. (c) 2005 Pearson Education. and for users to install. Introduction to Java Programming. There is no need for developers to create. Fifth Edition. All rights reserved.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 Java's Performance environment. Liang. New features can be incorporated transparently as needed. Java Is Dynamic major new software versions. 0-13-148952-6 43 . Inc.

1998)  Java 2 SDK v 1.4. 2004) Liang.JDK Versions  JDK 1.2.4 (a.5 (a. Fifth Edition.k.a JDK 1. All rights reserved.3.k. 0-13-148952-6 44 .02 (1995)  JDK 1.3 (a.a JDK 1.a JDK 1.1 (1996) major changes  Java 2 SDK v 1. 2000)  Java 2 SDK v 1. Inc.k. 2002)  Java 2 SDK v 1.2 (a.5. Introduction to Java Programming. (c) 2005 Pearson Education.k.a JDK 1.

Liang. All rights reserved. Introduction to Java Programming. (c) 2005 Pearson Education.  Java Enterprise Edition (J2EE) – J2EE can be used to develop server-side applications such as Java servlets and Java ServerPages.JDK Editions  Java Standard Edition (J2SE) – J2SE can be used to develop client-side standalone applications or applets. – J2ME can be used to develop applications for mobile devices such as cell phones. Inc. This book uses J2SE to introduce Java programming. 0-13-148952-6 45 . Fifth Edition.  Java Micro Edition (J2ME).

Inc.Java IDE Tools  Borland JBuilder  NetBeans Open Source by Sun  Sun ONE Studio by Sun MicroSystems  Eclipse Open Source by IBM Liang. Introduction to Java Programming. (c) 2005 Pearson Education. 0-13-148952-6 46 . All rights reserved. Fifth Edition.

(1) set c:\Program Files\java\jdk1. and (2) install slides from the Instructor Resource Website to a directory (e. Liang.out. c:\LiangIR) . 0-13-148952-6 47 . Inc. All rights reserved. } } Source Run IMPORTANT NOTE: To run the program from the Run button.0\bin for path.g. Fifth Edition.println("Welcome to Java!"). Introduction to Java Programming.A Simple Java Program Example 1..5. (c) 2005 Pearson Education.1 //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.

0-13-148952-6 48 .Creating and Editing Using NotePad To use NotePad. Introduction to Java Programming. Liang. (c) 2005 Pearson Education. type notepad Welcome.java from the DOS prompt. Inc. Fifth Edition. All rights reserved.

Inc.java from the DOS prompt. All rights reserved. Introduction to Java Programming.Creating and Editing Using WordPad To use WordPad. Fifth Edition. type write Welcome. 0-13-148952-6 49 . Liang. (c) 2005 Pearson Education.

println("Welcome to Java!"). not for you to understand) Compile Source Code i.. 0-13-148952-6 50 . Introduction to Java Programming. (c) 2005 Pearson Inc. and Running Programs Create/Modify Source Code Source code (developed by the programmer) Saved on the disk package chapter1.java If compilation errors stored on the disk … Method Welcome() 0 aload_0 … Method void main(java. java Welcome Result Liang.String[]) 0 getstatic #2 … 3 ldc #3 <String "Welcome to Java!"> 5 invokevirtual #4 … 8 return Bytecode Run Byteode i. } } Source Code Byte code (generated by the compiler for JVM to read and interpret.lang.Creating. Compiling. javac Welcome. Fifth Edition.. All If runtimeEducation. errors or incorrect result rights reserved.e.out. public class Welcome { public static void main(String[] args) { System.e.

Inc.5  See Supplement B for compiling and running Java from the command window for details www. Introduction to Java Programming. 0-13-148952-6 51 .prenhall.Supplements on the Companion Website  See Supplement A for installing and configuring JDK 1.armstrong.html Liang.cs. Fifth Edition. (c) 2005 Pearson Education.edu/liang/intro5e. All rights reserved.com/liang Direct link at www.

Introduction to Java Programming. Inc.  Compile – javac Welcome.0\bin – set classpath=.Compiling and Running Java from the Command Window  Set  Set path to JDK bin directory classpath to include the current directory – set path=c:\Program Files\java\jdk1.java  Run – java Welcome Liang. Fifth Edition. All rights reserved.5. 0-13-148952-6 52 . (c) 2005 Pearson Education.

Inc. All rights reserved.TextPad Optional Compiling and Running Java from TextPad Supplement B on the Website for details  See Liang. Fifth Edition. 0-13-148952-6 53 . Introduction to Java Programming. (c) 2005 Pearson Education.

Inc. All rights reserved. Introduction to Java Programming. Fifth Edition. (c) 2005 Pearson Education.JBuilder Optional Compiling and Running Java from JBuilder Supplement H on the Website for details  See Liang. 0-13-148952-6 54 .

Fifth Edition. 0-13-148952-6 55 . Introduction to Java Programming. (c) 2005 Pearson Education. All rights reserved.NetBeans Optional Compiling and Running Java from NetBeans Supplement I on the Website for details  See Liang. Inc.

All rights reserved.Anatomy of a Java Program  Comments  Package  Reserved  Modifiers words  Statements  Blocks  Classes  Methods  The main method Liang. Inc. 0-13-148952-6 56 . Fifth Edition. (c) 2005 Pearson Education. Introduction to Java Programming.

it scans for the next */ and ignores any text between /* and */. comments are preceded by two slashes (//) in a line. Inc. All rights reserved. it ignores all text after // in the same line.Comments In Java. Introduction to Java Programming. 0-13-148952-6 57 . or enclosed between /* and */ in one or multiple lines. (c) 2005 Pearson Education. When the compiler sees //. When it sees /*. Fifth Edition. Liang.

Forte compiles the source code in Welcome. Liang.) specifies a package name. (c) 2005 Pearson Education. generates Welcome. for the class Welcome.Package The second line in the program (package chapter1. All rights reserved. and stores Welcome. 0-13-148952-6 58 . chapter1.class. Fifth Edition. Inc. Introduction to Java Programming.java.class in the chapter1 folder.

For example. Fifth Edition. 0-13-148952-6 59 .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. static. it understands that the word after class is the name for the class. Liang. (c) 2005 Pearson Education. All rights reserved. and void. Other reserved words in Example 1. when the compiler sees the word class. Inc.1 are public. Their use will be introduced later in the book. Introduction to Java Programming.

method. and classes and how they can be used. A public datum.” Liang. “Objects and Classes. and protected. A private datum or method cannot be accessed by other programs. Inc. Modifiers are discussed in Chapter 6. abstract. 0-13-148952-6 60 .Modifiers Java uses certain reserved words called modifiers that specify the properties of the data. Fifth Edition. Other modifiers are private. methods. All rights reserved. (c) 2005 Pearson Education. Examples of modifiers are public and static. final. or class can be accessed by other programs. Introduction to Java Programming.

Statements A statement represents an action or a sequence of actions. All rights reserved. Liang.out. The statement System. 0-13-148952-6 61 . Introduction to Java Programming.). Inc.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 (. Fifth Edition. (c) 2005 Pearson Education.

out. 0-13-148952-6 62 .Blocks A pair of braces in a program forms a block that groups components of a program. } } Class block Method block Liang. All rights reserved. Fifth Edition. public class Test { public static void main(String[] args) { System.println("Welcome to Java!"). Introduction to Java Programming. (c) 2005 Pearson Education. Inc.

though. Inc. Fifth Edition.Classes The class is the essential Java construct. (c) 2005 Pearson Education. 0-13-148952-6 63 . The mystery of the class will continue to be unveiled throughout this book. A class is a template or blueprint for objects. Introduction to Java Programming. understand that a program is defined by using one or more classes. Liang. All rights reserved. you must understand classes and be able to write and use them. For now. To program in Java.

All rights reserved. It can be used even without fully understanding the details of how it works. The string argument is enclosed within parentheses. Fifth Edition. In this case.println? It is a method: a collection of statements that performs a sequence of operations to display a message on the console. Liang. 0-13-148952-6 64 .out. the argument is "Welcome to Java!" You can call the same println method with a different argument to print a different message. It is used by invoking a statement with a string argument.Methods What is System. (c) 2005 Pearson Education. Inc. Introduction to Java Programming.

Inc. Fifth Edition. (c) 2005 Pearson Education. 0-13-148952-6 65 . } Liang. All rights reserved. The Java interpreter executes the application by invoking the main method. The main method looks like this: public static void main(String[] args) { // Statements. Introduction to Java Programming.main Method The main method provides the control of program flow.

5. Introduction to Java Programming. Inc. (c) 2005 Pearson Education. (1) set c:\jdk1. Liang. and (2) install slides from the Instructor Resource Website to a directory (e..0\bin for path. Fifth Edition.Displaying Text in a Message Dialog Box you can use the showMessageDialog method in the JOptionPane class. 0-13-148952-6 66 .” Source Run IMPORTANT NOTE: To run the program from the Run button. c:\LiangIR) . All rights reserved.g. which can be reused rather than “reinventing the wheel. JOptionPane is one of the many predefined classes in the Java system.

0-13-148952-6 67 . JOptionPane. (c) 2005 Pearson Education. Introduction to Java Programming.INFORMATION_MESSAGE)). All rights reserved.showMessageDialog(null.The showMessageDialog Method JOptionPane.2 Output". "Example 1. "Welcome to Java!". Fifth Edition. Liang. Inc.

where x is a string for the text to be displayed. The other is to use a statement like this: JOptionPane.Two Ways to Invoke the Method There are several ways to use the showMessageDialog method. For the time being. Introduction to Java Programming. One is to use a statement as shown in the example: JOptionPane. Fifth Edition. y. all you need to know are two ways to invoke it. (c) 2005 Pearson Education. Inc. Liang.showMessageDialog(null. and y is a string for the title of the message dialog box. x. 0-13-148952-6 68 .showMessageDialog(null. All rights reserved. where x is a string for the text to be displayed. x).INFORMATION_MESSAGE)). JOptionPane.

(c) 2005 Pearson Education.5.The exit Method Prior to JDK 1. it is not necessary. Introduction to Java Programming. All rights reserved. Liang.5. Since JDK 1. you have to invoke System. 0-13-148952-6 69 .exit() to terminate the program if the program uses JOptionPane dialog boxes. Fifth Edition. Inc.

Fifth Edition. 0-13-148952-6 70 . (c) 2005 Pearson Education. All rights reserved.JBuilder Optional JBuilder IDE Interface file tab content pane (showing the editor) main menu main toolbar project toolbar project pane structure pane message pane execution status bar status bar file viewer tab Resize editor font Liang. Introduction to Java Programming. Inc.

You may optionally check this box to let the wizard generate a project description file. Introduction to Java Programming. This is an HTML file. All rights reserved. the wizard can create it automatically. which is nonessential for the project. You may optionally choose an existing project as template. All the files in the book are stored in c:\book. New Project to display the project wizard book C:\book C:\book Enter a directory name where your project will be stored. (c) 2005 Pearson Education. If the directory does not exist.JBuilder Optional Creating a JBuilder project Enter a project name -. Inc.any descriptive of your choice Choose File. 0-13-148952-6 71 . Fifth Edition. Liang.

backup path. Fifth Edition. Creating projects incorrectly is a common problem for new JBuilder users. There are many ways to set paths. The working directory is the starting directory that JBuilder gives a program when it is launched. cont. Set output path. I recommend new JBuilder users to choose the project path as the output path. and source path. Inc. working directory. The source directory is where the source files created using JBuilder‟s wizards are stored. and can lead to frustrating mistakes. Any directory may be configured as the working directory. I recommend you to follow the instructions from your instructor to set these paths.JBuilder Optional Creating a JBuilder project. 0-13-148952-6 72 . The backup path is where the backup files are stored. Introduction to Java Programming. However. (c) 2005 Pearson Education. Liang. I recommend you to enter outpath/bak. You can choose any directory as output path. By default. I recommend new JBuilder users to choose the project path as the output path. All rights reserved. it has the same name as the project file.

All rights reserved. Introduction to Java Programming. Inc. Fifth Edition.JBuilder Optional Creating a JBuilder project. 0-13-148952-6 73 . Optional project description Liang. (c) 2005 Pearson Education. cont.

All rights reserved. This is the default. All the examples in Introduction to Java Programming. e.. Introduction to Java Programming. (c) 2005 Pearson Education. Check these two boxes Liang. So leave this blank.JBuilder Optional Creating a Java Program Choose File. 5E do not use the package statement. Inc. Fifth Edition. New Class to display the class wizard Enter the class name.g. Leave it as it is. Welcome. Leave these boxes unchecked. 0-13-148952-6 74 .