18
Introduction to Java Introduction to Java Programming Programming Lecture 1: Lecture 1: Introduction To Java And Computers Introduction To Java And Computers Spring Spring 2008 2008

Introduction to Java Programming Lecture 1: Introduction To Java And Computers Spring 2008

  • View
    235

  • Download
    3

Embed Size (px)

Citation preview

Introduction to Java ProgrammingIntroduction to Java Programming

Lecture 1: Lecture 1:

Introduction To Java And ComputersIntroduction To Java And Computers

Spring 2008Spring 2008

Text Book For Lab (Required)Text Book For Lab (Required)

最新 Java2

程式設計實例入門 ( 增訂第三版 )

作者 : 高橋麻奈

Book includes a CD-ROM with Java programs and other supplemental materials.

博碩文化 出版2

4

Text Book RecommendedText Book Recommended Introduction to Java

Programming (6th Edition), by D. Liang

Book includes a CD-ROM with Java programs and other supplemental materials.

全華圖書代理

4

SoftwareSoftware For the course, we will be using the following

software :

– Sun’s JDK Java Development Kit, JDK 6 Update x (Required)

– The JCreator IDE (Will be used later in the semester) (IDE : Integrated Development Environment)

– NetBeans IDE (Optional)

All three products can be downloaded from the web for free.

5

6

What Is a Computer?What Is a Computer?

Computer– 可以輸入、儲存資料,可以對資料進行計算與邏

輯運算,可以輸出、顯示資料的機器 Computer programs

– 由一行一行的程式組成,用來處理資料 Hardware

– 電腦的實體設備,包括 : 主機板、 CPU 、顯示卡、硬碟、記憶體、光碟機 … 等

Software– 可以在電腦上執行的 Programs

7

Computer OrganizationComputer Organization Six logical units of computer system

– Input unit ( 輸入 ) 滑鼠、鍵盤

– Output unit ( 輸出 ) 螢幕、印表機、喇叭

– Memory unit ( 記憶體 ) 電腦運作期間,用來放置資料

– Arithmetic and logic unit (ALU) 執行計算與邏輯的運算

– Central processing unit (CPU) 控制、指揮 軟硬體的運作

– Secondary storage unit ( 輔助儲存設備 ) 硬碟、 Floppy disk

2003 Prentice Hall, Inc. All rights reserved.

8

Computer InstructionsComputer Instructions 計算機程式 (Computer programs)由一行一行的程式指令 (instructions) 組成

指令以二進位 (binary) 的數字存放在記憶體(RAM) 中,稱之為機器語言 (machine code)

例如 01001010 00001111 “add to” “register 15“

( 加到 ) ( 暫存器 15)

指令也可能包含資料 (data) 例如 01001010 00001111

00000000 01100100 (Add 100 to register 15)

9

Computer InstructionsComputer Instructions

記憶體中的每一個位元組 (byte) 都有相對應的地址

Address Contents 200 01001010 201 00001111 202 00000000 203 01100100 204 01011011 205 01100101

one computer instruction

another instruction

10

Execute a programExecute a program

執行 (execute) 一個程式時,作業系統 (The operating system : Windows, Linux or MacOS) 有一個工具程式叫做 loader ,會把程式從硬碟複製到記憶體中,我們只要“告訴” CPU 程式第一行的地址,然後讓程式從第一行開始執行就行了

但是我們怎麼知道什麼二進位的數字可以組成一個我們希望電腦執行的程式呢

這就是我們學像 Java 這種程式語言的目的

11

Programming LanguagesProgramming Languages

高階程式語言 (High-level programming languages) 是利用我們平常所使用的語言 ( 英文 ) 及符號來寫程式

編譯器 (compiler) 是一個程式,可以把高階程式語言轉換成機器語言

C++ C Fortran Java 都是高階程式語言

12

Typical CompilationTypical Compilation

z = x + y;

10010101 01100001 (load x into reg 1) 10100110 11010001 (add y to reg 1) 10111100 01010001 (store reg 1 into z)

但是 : compiler 是針對某一種 CPU 設計的,所以這些機器語言的指令只能給這種 CPU 執行

編譯器把這些指令編譯成機器碼 :

13

Java CompilationJava Compilation

z = x + y;

0001 1010 (iload_0) 1010 1011 (iload_1) 0110 0000 (iadd) 0011 1101 (istore_2)

編譯器把這些指令編譯成“虛擬”的機器碼 (“virtual“ machine code) ,稱為“位元碼“ (“byte codes”) :

14

Java ExecutionJava Execution

位元碼可以經由直譯器 (Java interpreter) 來執行Java interpreter (JVM - Java virtual machine) 可以把位元碼轉換成適用於某種 CPU 的機器碼

所以,不管是哪一種電腦,只要這個電腦有安裝適用於這個電腦的 JVM ,就能執行任何的 Java byte codes

History of JavaHistory of Java

A group of 13 Sun employees including James Gosling started the “Green Project” in 1991 with the intention of planning for the next wave in computing.

Gosling’s contribution to the project was an entirely new processor independent language call “Oak”.

15

16

History of Java (continued)History of Java (continued) To make a long story short, people at Sun

decided to use this new language for the web. At the Sun World conference in May 1995, Marc

Andreessen of Netscape announced an agreement to integrate Java into its browser. This meant that webpages were no longer going to be static.

Over the next few years, java became very popular for writing applets (small programs included on webpages)

Today in addition to writing applets, Java is used for writing large applications as well as applications for mobile devices

For more on the history of Java, check out: http://java.sun.com/features/1998/05/birthday.html

17

Basics of a Typical Java EnvironmentBasics of a Typical Java Environment Java programs normally undergo five phases (Java 程式的執行 )

– Edit ( 編輯 )Programmer writes program (and stores program on disk)

– Compile ( 編譯 )Compiler creates bytecodes from program

– Load ( 載入 )Class loader stores bytecodes in memory

– Verify ( 安全性驗證 )Verifier ensures bytecodes do not violate security

requirements– Execute ( 直譯與執行 )

Interpreter translates bytecodes into machine language

2003 Prentice Hall, Inc. All rights reserved.

Another Basic Step for Java ProgrammingAnother Basic Step for Java Programming

Debugging ( 除錯 )

– Check program execution and output to ensure program compiles and runs as expected

– If it doesn’t, make corrections in the edit phase and repeat the remaining steps

18