1 introduction of java concept. 2 reference tarted/intro/definition.html java2...

41
1 Introduction of Java Concept

Post on 20-Dec-2015

225 views

Category:

Documents


2 download

TRANSCRIPT

1

Introduction of Java Concept

2

Reference

• http://java.sun.com/docs/books/tutorial/getStarted/intro/definition.html

• Java2 物件導向程式設計,洪維恩著,博碩文化。

3

Outlines

• Introduction

• Java Programming Language

• Java Platform

• Java Program

4

Introduction

5

Birth of Java

• Java 於 1995 年誕生,它是由美國加州的昇陽電腦公司 (Sun Microsystems, Inc.)所推出,是一種能夠跨平台 (cross platform) 使用的程式語言。

6

Java History

• Oak 是 Java 語言的前身,但在一次爭取電視機上的電腦控制器專案失敗, Sun 將 Oak 改為 Java ,並將這種技術轉移到 Web 網路上。

• Web 於 1994 年在 Internet 上開始盛行,這使得 Java 工作小組靈機一動,將 Java 應用到 Web 上,沒想到這個舉動竟使 Java 一炮而紅,變成 Internet 上最閃亮的巨星。

7

Java Version

• J2SE 是 Java 最通行的版本。• J2ME( 精簡版, micro edition)

– J2ME 是行動商務最佳的應用典範,不論是無線通訊、手機、 PDA … 等小型電子裝置均可採用 J2ME 做為開發工具及應用平台。

• J2EE ( 企業版, enterprise edition) 。– J2EE 則提供了企業 e-Business 架構及 Web Serv

ices 服務,其優越的跨平台能力與開放的標準,深受廣大企業用戶的喜愛。

J2EE

J2ME

J2SE

8

Java Technology

• Java technology is both a programming language and a platform.– Java programming– Java platform

Java Program

9

Java Platform

10

Java Platform Architecture

• The Java platform has two components: – The Java Virtual Machine (Java VM) – The Java Application Programming Interface

(Java API)

Java Program

11

Java Virtual Machine

• 任何一種可以執行 Java bytecode 的軟體均可看成是 Java 的「虛擬機器」 (Java virtual machine , JVM) 。

12

Java API

• The Java API is a large collection of ready-made software components that provide many useful capabilities.– Example : graphical user interface (GUI)

• The Java API is grouped into libraries of related classes and interfaces; these libraries are known as packages.

13

Java Environment

• The Java 2 Platform, Standard Edition – Download the SDK, http://java.sun.com

• A text editor

14

JDK (Java Development Kit)

The Java 2 SDK, Standard Edition v. 1.3.

15

Features of Java Platform (1/3)

• Every full implementation of the Java platform gives you the following features: – The essentials: Objects, strings, threads,

numbers, input and output, data structures, system properties, date and time, and so on.

– Applets: The set of conventions used by applets. – Networking: URLs, TCP, UDP sockets, and IP

addresses.

16

Features of Java Platform (2/3)

– Internationalization: Help for writing programs that can be localized for users worldwide. Programs can automatically adapt to specific locales and be displayed in the appropriate language.

– Security: electronic signatures, public and private key management, access control, and certificates.

– Software components: JavaBeansTM

17

Features of Java Platform (3/3)

– Object serialization: Allows lightweight persistence and communication via Remote Method Invocation (RMI).

– Java Database Connectivity (JDBCTM): Provides uniform access to a wide range of relational databases.

– The Java platform also has APIs for 2D and 3D graphics, accessibility, servers, collaboration, telephony, speech, animation, and more

18

Java Program

19

Java Programming Language

• A high-level language

• Characteristics – Simple– Architecture neutral– Object oriented– Portable– Distributed– High performance

– Interpreted– Multithreaded– Robust– Dynamic– Secure

20

Applications and Applets

• Two type of Java programs:– Applications: standalone programs, run on the

SDK– Applets: similar to applications, but run within

a Java-compatible browser.

21

Simple Application Sample

• The "Hello Java" Application /* simple hello java application */class HelloJavaApp {

public static void main(String[] args) { System.out.println("Hello Java!"); //Display the string.

}

}

• Saving files– File name must be class name with .java extension

HelloJavaApp.java

22

Execute The Program

23

Java Develop Environment

Editor

Compiler

Class Loader

app1_1.java source code

Bytecode Verifier

Interpreter

app1_1.class bytecodes

Hello Java !! in screen

24

General Compilers

• With most programming languages, you either compile or interpret a program so that you can run it on your computer.

• The Java programming language is unusual in that a program is both compiled and interpreted.

25

Java Complier and Interpreter

• With the Java compiler, first you translate a program into an intermediate language called Java bytecodes.

• The interpreter parses and runs each Java bytecode instruction on the computer. – Compilation happens just once. – Interpretation occurs each time the program is

executed.

26

Java Bytecodes

• Java bytecodes is platform-independent codes interpreted by the interpreter on the Java platform.

Java Platform

Java Bytecodes

Java Program

27

Java Interpreter

• Every Java interpreter is an implementation of the Java VM. – JDK ( Java Development Kit )– A Web browser that can run applets

28

Platform Independent

• Write once, run anywhere.

HelloWorldApp.class

29

app1_1.java

/** * The app1_1.java implements an application that simply displays "Hello Java !!" to the standard output. */

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

30

Your First Program (1/2)

• 把程式鍵入記事本中,並把它存入 C:\Java 資料夾內,檔名設為 app1_1.java :

• Dos 視窗開啟後,請先將資料夾切換到儲存 app1_1.java 的 C:\Java 資料夾中,亦即在 Dos 視窗內鍵入:

> cd C:\Java• 請執行下面的指令來編譯 app1_1.java : > javac app1_1.java

31

Your First Program (2/2)

• 編譯好了之後,您可以在 C:\Java 資料夾內發現一個檔名一樣是 app1_1 ,但副檔名為「 .class 」的檔案。這個檔案也就是前一節所說的 byte-codes 。

• 請執行下面的指令來執行 byte-codes ( 即 app1_1.class) :

> java app1_1

32

Simple Applet Sample

• The "Hello Java" Applet import java.applet.Applet;

import java.awt.Graphics;

public class HelloApplet extends Applet { public void paint(Graphics g) {

g.drawString("Hello Applet!", 50, 25);

}

}

33

“HelloWorld” Applet

import java.applet.Applet; import java.awt.Graphics; public class HelloWorld extends Applet { public void paint(Graphics g) { g.drawString("Hello world!", 50, 25); } }

34

Hello.html

1. <HTML> 2. <HEAD> 3. <TITLE> A Simple Applet Program</TITLE> <

/HEAD> 4. <BODY> Here is the output of my program:5. <APPLET CODE="HelloApplet.class" WIDT

H=150 HEIGHT=25> 6. </APPLET> 7. </BODY> 8. </HTML>

35

Running an Applet

• javac HelloWorld.java

• appletviewer Hello.html

• 或用 web browser to open Hello.html

36

The Life Cycle of an Applet

1. import java.applet.Applet;2. import java.awt.Graphics;3. public class Simple extends Applet {4. public void init() {5. }6. public void start() {7. }8. public void stop() {9. }10. public void destroy() {11. }12. public void paint(Graphics g) {13. }14.}

37

Compare With Native Code• Native code is code that after you compile it, the

compiled code runs on a specific hardware platform.

• As a platform-independent environment, the Java platform can be a bit slower than native code. However, smart compilers, well-tuned interpreters, and just-in-time bytecode compilers can bring performance close to that of native code without threatening portability.

38

Special Java Program

• A special kind of application known as a server serves and supports clients on a network.

• Another specialized program is a servlet. Servlets run within Java Web servers, configuring or tailoring the server.

39

Java Servlet

• Java Servlet : a Java class that processes requests placed by a client to a web server

Virtual Machine (JVM)

web server + servlet engine

data sources, external

applications, other

servlets, etc.

Device OS

Client

HTTP request

HTTP response

servlet

40

Summary - Java Features

• Java 的語言特色有下列幾點,簡單,物件導向,符合網路環境,直譯式,不易出錯,安全,跨平台,可攜性,高效能,多執行緒,動態連結。

Windows NT/98

Unix/ FreeBSD/ Linux

Mac OS

Java Virtual Machine

application

41

Summary - Advantages of Java

• Java technology help you do the following :– Get started quickly– Write less code– Write better code – Develop programs more quickly– Avoid platform dependencies with 100% Pure Java – Write once, run anywhere– Distribute software more easily