Download presentation
Presentation is loading. Please wait.
Published byDustin Harrison Modified over 9 years ago
1
Chapter 1 Introduction to Computers, Programs, and Java 1
2
Objectives To review computer basics, programs, and operating systems (§1.2-1.4). To understand the relationship between Java and the World Wide Web (§1.6). To know Java’s advantages (§1.7). To distinguish the terms API, IDE, and JDK (§1.8). To write a simple Java program (§1.9). To create, compile, and run Java programs (§1.10). To understand the Java runtime environment (§1.10). To know the basic syntax of a Java program (§1.11). To display output on the console and on the dialog box (§1.12). 2
3
What is a Computer? 3 A computer consists of a CPU, memory, hard disk, floppy disk, monitor, printer, and communication devices.
4
CPU 4 The central processing unit (CPU) is the brain of a computer. It retrieves instructions from memory and executes them. The CPU speed is measured in megahertz (MHz), with 1 megahertz equaling 1 million pulses per second. The speed of the CPU has been improved continuously. If you buy a PC now, you can get an Intel Pentium 4 Processor at 3 gigahertz (1 gigahertz is 1000 megahertz).
5
Memory 5 Memory is to store data and program instructions for CPU to execute. A memory unit is an ordered sequence of bytes, each holds eight bits. A program and its data must be brought to memory before they can be executed. A memory byte is never empty, but its initial content may be meaningless to your program. The current content of a memory byte is lost whenever new information is placed in it.
6
How Data is Stored? Data of various kinds, such as numbers, characters, and strings, are encoded as a series of bits (zeros and ones). Computers use zeros and ones because digital devices have 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 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. 6
7
Storage Devices 7 Memory is volatile, because information is lost when the power is off. Programs and data are permanently stored on storage devices and are moved to memory when the computer actually uses them. There are three main types of storage devices:Disk drives (hard disks and floppy disks), CD drives (CD-R and CD-RW), and Tape drives.
8
Output Devices: Monitor 8 The monitor displays information (text and graphics). The resolution and dot pitch determine the quality of the display.
9
Programs Computer programs, known as software, are instructions to the computer. You tell a computer what to do through programs. Without programs, a computer is an empty machine. Computers do not understand human languages, so you need to use computer languages to communicate with them. Programs are written using programming languages. 9
10
Programming Languages Machine Language Assembly Language High-Level Language 10 Machine language primitive instructions built into every computer the only language the computer understands! varies across platforms form: binary code
11
Programming Languages Machine Language Assembly Language High-Level Language 11 High-level languages English-like Easy to learn and program Must be translated to machine code before execution Compilation Vs. Interpretation
12
Java Byte-Code Output of Java compiler: byte-code, not executable code. Running Byte-Code: Java Virtual Machine (JVM) – a software simulation of a CPU whose machine language is byte-code. Thus, byte-code enables the JVM to execute faster than interpreters typically do. Just In Time (JIT) compiler – compiles Java byte-code into machine code before execution. Java chip – Java byte-code is the machine code of the device. Advantages High degree of platform independence Fast introduction to new platforms 12
13
Why Java? 13 The answer is that Java enables users to deploy applications on the Internet for servers, desktop computers, and small hand-held devices. The future of computing will be profoundly influenced by the Internet, and Java promises to remain a big part of that future. F Java is a general purpose programming language. F Java is the Internet programming language.
14
Java, Web, and Beyond Java can be used to develop Java desktop applications. Java Applets Java Servlets and JavaServer Pages applications for hand-held devices (i.e., Palm, cell phones) 14
15
Java’s History James Gosling and Sun Microsystems Oak Java, May 20, 1995, Sun World HotJava ▫The first Java-enabled Web browser Early History Website: 15 http://java.sun.com/features/1998/05/birthday.html
16
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 16 Optional
17
Characteristics of Java Java Is Simple Java Is Object-Oriented Java Is Distributed Java Is Interpreted Java Is Robust Java Is Secure Java Is Architecture- Neutral Java Is Portable Java's Performance Java Is Multithreaded Java Is Dynamic 17 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 functionality and fewer negative aspects.
18
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 18 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. Object- oriented programming provides great flexibility, modularity, clarity, and reusability through encapsulation, inheritance, and polymorphism.
19
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 19 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.
20
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 20 You need an interpreter to run Java programs. The programs are compiled into the Java Virtual Machine code called bytecode. 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).
21
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 21 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.
22
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 22 Java implements several security mechanisms to protect your system against harm caused by stray programs.
23
Characteristics of Java Java Is Simple Java Is Object-Oriented Java Is Distributed Java Is Interpreted Java Is Robust Java Is Secure Java Is Architecture- Neutral Java Is Portable Java's Performance Java Is Multithreaded Java Is Dynamic 23 Write once, run anywhere With a Java Virtual Machine (JVM), you can write one program that will run on any platform.
24
Characteristics of Java Java Is Simple Java Is Object-Oriented Java Is Distributed Java Is Interpreted Java Is Robust Java Is Secure Java Is Architecture- Neutral Java Is Portable Java's Performance Java Is Multithreaded Java Is Dynamic 24 Because Java is architecture neutral, Java programs are portable. They can be run on any platform without being recompiled.
25
Characteristics of Java Java Is Simple Java Is Object-Oriented Java Is Distributed Java Is Interpreted Java Is Robust Java Is Secure Java Is Architecture- Neutral Java Is Portable Java's Performance Java Is Multithreaded Java Is Dynamic 25 Java ’ s performance Because Java is architecture neutral, Java programs are portable. They can be run on any platform without being recompiled.
26
Characteristics of Java Java Is Simple Java Is Object-Oriented Java Is Distributed Java Is Interpreted Java Is Robust Java Is Secure Java Is Architecture- Neutral Java Is Portable Java's Performance Java Is Multithreaded Java Is Dynamic 26 Multithread programming is smoothly integrated in Java, whereas in other languages you have to call procedures specific to the operating system to enable multithreading.
27
Characteristics of Java Java Is Simple Java Is Object-Oriented Java Is Distributed Java Is Interpreted Java Is Robust Java Is Secure Java Is Architecture- Neutral Java Is Portable Java's Performance Java Is Multithreaded Java Is Dynamic 27 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.
28
JDK Versions JDK 1.02 (1995) JDK 1.1 (1996) major changes Java 2 SDK v 1.2 (a.k.a JDK 1.2, 1998) Java 2 SDK v 1.3 (a.k.a JDK 1.3, 2000) Java 2 SDK v 1.4 (a.k.a JDK 1.4, 2002) Java 2 SDK v 1.5 (a.k.a JDK 1.5, 2004) 28
29
JDK Editions Java Development Kit is the software distribution containing tools for developing and running Java applications Java Standard Edition (J2SE) ▫ Desktop applications or applets. ▫ We will be using! Java Enterprise Edition (J2EE) ▫ Server-side applications (i.e., Java servlets and Java ServerPages). Java Micro Edition (J2ME) ▫ Applications for mobile devices such as cell phones. 29
30
JRE JRE – Java Runtime Environment Software and classes for running Java application s API vs. JDK vs. JRE 30
31
Code Development The most primitive (and simplest) development environment under Windows: use Notepad and the Command Prompt. Edit your code using Notepad Compile (using javac) to create a class file Javac myProgram.java Execute (using java) Java myProgram 31
32
Java IDE Tools Borland JBuilder NetBeans Open Source by Sun Sun ONE Studio by Sun MicroSystems Eclipse Open Source by IBM 32
33
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!"); } 33 Example 1.1
34
Supplements on the Companion Website See Supplement A for installing and configuring JDK 1.5 See Supplement B for compiling and running Java from the command window for details www.prenhall.com/liang Direct link at www.cs.armstrong.edu/liang/intro5e.htm l 34
35
Compiling and Running Java from the Command Window Set path to JDK bin directory set path=c:\Program Files\java\jdk1.5.0\bin Set classpath to include the current directory set classpath=. Compile javac Welcome.java Run java Welcome 35
36
Anatomy of a Java Program Comments Reserved words Modifiers Statements Blocks Classes Methods The main method 36
37
Java Comments Line comments: following // in one line Block comments: between /* and */ Javadoc comment between /** and */ To document a class or method can be extracted into HTML file using javadoc command 37
38
Reserved Words Reserved words or keywords are words that have a specific meaning to the compiler and cannot be used for other purposes in the program. For example, when the compiler sees the word class, it understands that the word after class is the name for the class. Other reserved words in Example 1.1 are public, static, and void. Their use will be introduced later in the book. 38
39
Statements A statement represents an action or a sequence of actions. The statement System.out.println("Welcome to Java!") in the program in Example 1.1 is a statement to display the greeting "Welcome to Java!" Every statement in Java ends with a semicolon (;). 39
40
Blocks 40 A pair of braces in a program forms a block that groups components of a program.
41
Classes The class is the essential Java construct. A class is a template or blueprint for objects. To program in Java, you must understand classes and be able to write and use them. The mystery of the class will continue to be unveiled throughout this book. For now, though, understand that a program is defined by using one or more classes. 41
42
Methods What is System.out.println? It is a method: a collection of statements that performs a sequence of operations to display a message on the console. It can be used even without fully understanding the details of how it works. It is used by invoking a statement with a string argument. The string argument is enclosed within parentheses. In this case, the argument is "Welcome to Java!" You can call the same println method with a different argument to print a different message. 42
43
main Method The main method provides the control of program flow. The Java interpreter executes the application by invoking the main method. The main method looks like this: public static void main(String[] args) { // Statements; } 43
44
Applet Runs on client's machine from within a Web page Compilation process is the same as for an application javac HelloWorldApplet.java 44
45
A Simple Java Applet 45 //this applet prints “Hello World” in a rectangle import javax.swing.JApplet; import java.awt.Graphics; public class HelloWorld extends JApplet { public void paint(Graphics g) { g.drawRect(0, 0, getSize().width - 1, getSize().height - 1); g.drawString("Hello world!", 5, 15); }
46
Deploying an Applet Add applet tag to body of html file View the Web page Open html file in browser appletviewer utility that comes with the JDK appletviewer HelloWorldApplet.html 46
47
Applet - Differences No main method Paint method (function) is called when the applet needs to be displayed extends keyword means that our applet is inherited from the Applet class (more about inheritance later!) 47
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.