Presentation is loading. Please wait.

Presentation is loading. Please wait.

[2-1] Programming in Java Introduction to Java and Java Program Lecture2.

Similar presentations


Presentation on theme: "[2-1] Programming in Java Introduction to Java and Java Program Lecture2."— Presentation transcript:

1 [2-1] Programming in Java Introduction to Java and Java Program Lecture2

2 [2-2] Programming in Java Objectives What is Java ?  A programming language  A platform(Java APIs, JVM) Java’s History Java Technology Java vs. C++ Java program

3 [2-3] Programming in Java What is Java? Java is a programming language  general purpose  object-oriented Java is a development environment  Java APIs Java is an application environment  Java Virtual Machine Java is a deployment environment  Platform Independence

4 [2-4] Programming in Java The Features of Java Language [SUN] The Java Language :  simple  object-orientedobject-oriented  network-savvy  interpretedinterpreted  robustrobust  securesecure  architecture-neutralarchitecture-neutral  high-performance  multithread multithread  dynamic language

5 [2-5] Programming in Java In traditional computer languages, data are separated from procedures as in C, Pascal, …(procedural language) Prolog, and(declarative language) LISP(functional language) Object-oriented Data files procedure1procedure2procedureN Traditional languages: data procedures Java: In Java, data are encapsulated by the procedures defined over them return

6 [2-6] Programming in Java The complier translates Java source code into bytecode; the interpreter processes bytecode when you run a Java program Interpreted return

7 [2-7] Programming in Java The Java platform ensures that programs run correctly and do not break when the unexpected happens  Strongly typing  Lack of pointer  Garbage collection  Exception handling Robust return

8 [2-8] Programming in Java Secure Lack of memory access directly Byte code verification Applets in the sandbox Security Manager return

9 [2-9] Programming in Java Provides architecture neutrality in part by allowing no implementation-defined features bytecode is portable because it runs on all implementations of the JVM Architecture-neutral and Portable return

10 [2-10] Programming in Java Multithreaded Multithreading can be loosely defined as the simultaneous execution of multiple processes within a program Java is inherently multi-threaded  For example, multiple applets can run on the same web page with each applet receiving more or less equal CPU time return

11 [2-11] Programming in Java The Java platform includes:  Java Virtual Machine (JVM)  A number of packages that support the complier, interpreter, and run-time environment for Java (APIs) Overview of the Java Platform

12 [2-12] Programming in Java What Is a Java Virtual Machine? Java Virtual Machine –the abstract specification – a concrete implementation – a runtime instance Starting of Java VM – start point:main() method of some initial class – main() method: public, static, return void – accept one parameter: a String array – public static void main(String[] args)

13 [2-13] Programming in Java The Architecture of the Java VM Class loader subsystem – read class files Execution engine –Use native method interface Runtime data area –Method area –Heap –Java Stack –PC registers –Native method stacks

14 [2-14] Programming in Java Garbage Collection Basic idea –keep track of what memory is referenced and when it is no longer accessible, reclaim the memory Garbage collector’s function –Finds data objects that are no longer in use(garbage) –Make garbage space available for reuse

15 [2-15] Programming in Java Garbage Collection in Java JVM spec. doesn’t require any garbage collection Garbage collection thread –Usually runs with low priority JDK1.0.2 and 1.1 uses a partially conservative mark-and –sweep garbage collector Garbage detection = reachability test –An object that are reachable from the roots are considered “live” No dangling reference

16 [2-16] Programming in Java Security-Related Capabilities(1) JDK 1.0 (Fall 1995) –Policy: “Sandbox” for applets; others unlimited –Mechanisms: SecurityManager, Bytecode verifier, Classloader JDK 1.1 (Spring 1997) –Policy: can also grant total trust to signed applets –Mechanisms: Java Archive (JAR), crypto- related APIs Inflexible: Too little or too much privilege

17 [2-17] Programming in Java Security-Related Capabilities(2) Netscape & Microsoft Extensions –Enabled more flexible approaches –Incompatible with each other and with Sun J2SE (Java 2 Platform Standard Edition) (Fall 1998) –Includes SDK 1.2 and runtime –Policy: can also grant fine-grained privileges to specific applets/classes based on source and/or signatures –Mechanisms: AccessController, ProtectionDomain, CodeSource, Permission, GuardedObject, … –“Java Plug-in” supports both Microsoft & Netscape

18 [2-18] Programming in Java Security Framework-JDK1.0

19 [2-19] Programming in Java What is Sandbox? Class loader,byte-code verifier and security manager Java protected domains use of permissions by user

20 [2-20] Programming in Java Security Framework-JDK1.1

21 [2-21] Programming in Java Security Framework-JDK1.2 No built-in notion of trusted code,each applet runs with separate permissions

22 [2-22] Programming in Java (1) Language: string, thread, exceptions, math, etc. (2) Utilities: hash table, stack, array, time, date (3) Input /Output Streams: file, network, device (4) Network Facilities: socket, TCP/IP, Internet (5) Graphics and Graphical User Interface(GUI): window, menu, bar, dialogue Class Library- Java Core APIs

23 [2-23] Programming in Java Where is the Java Platform Used?  to write small programs that run in Web pages  to produce Java classes that can act as extensions to your Web server  to distribute programs over a network  to use Java-based technology to meet the demands of Enterprise-wide distributed environments Where Is the Java Platform Used?

24 [2-24] Programming in Java Kinds of Java Platform Base Java platform –Network computers, Desktop computers, Workstations –Windows, Macintosh, OS/2, Linux, Unix, Netware, etc… Embedded Java platform –Set-top box, cellular phones, Java Card, etc… JavaOS, Java Processor

25 [2-25] Programming in Java January 1996: first official release JDK 1.0 Web: applets, security, URL, networking GUI: Abstract Windows Toolkit (AWT) Brief History of Java February 1997: JDK 1.1 1991:Created by James Gosling et. al. at Sun Microsystems "The Green Team" – Called the language 'Oak' 1994:Bill Joy "HotJava" browser 1995: Netscape agrees to support Java in Netscape browsers December 1998: Java 2 Platform (JDK 1.2)

26 [2-26] Programming in Java Java 2 Architecture

27 [2-27] Programming in Java Java2(1) J2SE(Java2 Standard Edition)J2SE –Basic Java specification for desktop computer development –Runs in the JVM J2EE(Java2 Enterprise Edition)J2EE –Multi-tier, highly available, secure, reliable and scalable – Requires different Java enterprise technologies – Runs in the JVM J2ME(Java2 Micro Edition)J2ME –set-top box, Java Card, etc…. –Runs in the KVM

28 [2-28] Programming in Java Java2(2) 返回

29 [2-29] Programming in Java J2EE Model 返回 ClientNetwork EIS 层 Integrate Business

30 [2-30] Programming in Java J2ME 返回

31 [2-31] Programming in Java Java Technology Development Environments Optional Packages Other Java Technologies

32 [2-32] Programming in Java (1) Java Developer’s Kits(JDK) Compiler javac java Interpreter jdbDebugger appletviewer Applet Viewer javah javadoc Generator of head file Document Administrator Java archive file Generatorjar Java IDE Tools(1) Get it from http://www.java.sun.com.

33 [2-33] Programming in Java (2) VisualAge for Java This environment provides : a convenient way to enter and save Java programs, both standalone applications and applets a debugging facility an excellent graphic interface which allows students to create applets which support graphical user interfaces(GUIs) (3) Inprise JBuilder (4) Microsoft Visual J++ (5) Symantec Café (6) Sun Java Workshop Java IDE Tools ( 3 )

34 [2-34] Programming in Java From Source Code to Running Program

35 [2-35] Programming in Java Java Optional Packages Java Mail Java 3D Java Media Framework(JMF) Java Servlet Java Cryptography Extension(JCE) Java Secure Socket Extension(JSSE)

36 [2-36] Programming in Java Different environments require different kinds of Java programs: — Applications: can run standalone on a computer of any size, and can be anything from large suites of software to simple utilities — Applets: are programs that can be launched from HTML documents ( client-side code that runs in a Web browser) — Servlets: run on Java-enabled Web servers and generate HTML documents that are sent to the client browser for display ( sever-side code that runs in a Web server) — JavaBeans: are Java classes or groups of Java classes that conform to a strict set of standards(reusable components that can be manipulate programmatically) Java Programs

37 [2-37] Programming in Java Applet and Servlet BrowserWeb Server JVM Applet 1 Applet 2 Servlet www.abc.com requests HTML files applets indication Applet 3

38 [2-38] Programming in Java Applications(1) foo.java Java program source code

39 [2-39] Programming in Java Applications(2) javac foo.java Java compiler

40 [2-40] Programming in Java Applications(3) javac foo.java foo.class Program bytecode

41 [2-41] Programming in Java Applications(4) javac foo.java foo.class java Java bytecode interpreter

42 [2-42] Programming in Java Applets(1) javac foo.java foo.class Same procedure as for Applications

43 [2-43] Programming in Java Applets(2) javac foo.java foo.class Netscape Your favorite browser

44 [2-44] Programming in Java Your First Java Application(1) /* Display a message */ public class HelloWorld { public static void main(String[] args){ System.out.println("Hello World!"); } } HelloWorld.java

45 [2-45] Programming in Java Your First Java Application(2) /* Display a message */ class HelloWorld { public static void main(String[] args){ System.out.println("Hello World!"); } } HelloWorld.java This will be the name of the resulting.class file

46 [2-46] Programming in Java Your First Java Application(3) C:\> javac HelloWorld.java C:\> java HelloWorld This produces HelloWorld.class This runs HelloWorld.clas s

47 [2-47] Programming in Java Your First Java Applet(1) import java.applet.Applet; import java.awt.Graphics; public class helloX extends Applet { public void paint(Graphics g) { g.drawString("Hello world!", 50, 25); } } helloX.java

48 [2-48] Programming in Java Your First Java Applet (2) C:\> javac helloX.java This produces hello.class (like before)

49 [2-49] Programming in Java Your First Java Applet (3) C:\> javac helloX.java This produces hello.class (like before) but then...

50 [2-50] Programming in Java Your First Java Applet(4) A Simple Program Here is the output of my program: <APPLET CODE="helloX.class" WIDTH=150 HEIGHT=25> A Simple Program Here is the output of my program: <APPLET CODE="helloX.class" WIDTH=150 HEIGHT=25> hellotest.html

51 [2-51] Programming in Java Your First Java Applet(5) Appletviewer hellotest.html

52 [2-52] Programming in Java A Program Analysis /* Display a message */ class hello { public static void main(String[] args){ System.out.println("Hello World!"); } }

53 [2-53] Programming in Java Functions Java program consists of a named class. /* Display a message */ class hello { public static void main(String[] args){ System.out.println("Hello World!"); } }

54 [2-54] Programming in Java Functions Java program consists of a named class. The body of the class is surrounded by braces /* Display a message */ class hello { public static void main(String[] args){ System.out.println("Hello World!"); } }

55 [2-55] Programming in Java Functions (Almost) every Java program must have one main() function. /* Display a message */ class hello { public static void main(String[] args){ System.out.println("Hello World!"); } }

56 [2-56] Programming in Java Functions (Almost) every Java program must have one and only one main() function. The body of the function is surrounded by braces /* Display a message */ class hello { public static void main(String[] args){ System.out.println("Hello World!"); } }

57 [2-57] Programming in Java Statements A semicolon is a statement terminator (not a statement separator). /* Display a message */ class hello { public static void main(String[] args){ System.out.println("Hello World!"); } }

58 [2-58] Programming in Java Statements Statements can be combined within braces to form a compound statement. White space is (almost always) ignored. /* Display a message */ class hello { public static void main(String[] args){ System.out.println("Hello World!"); } }

59 [2-59] Programming in Java Objects The identifier System.out is an object. The identifier println is one of the methods for that object. /* Display a message */ class hello { public static void main(String[] args){ System.out.println("Hello World!"); } }

60 [2-60] Programming in Java Strings "Hello World" is called a string There is an explicit string type in Java (unlike C++) Strings are different than characters /* Display a message */ class hello { public static void main(String[] args){ System.out.println("Hello World!"); } }

61 [2-61] Programming in Java Preprocessor Directives Public: indicates that this function can be called by objects outside of the class /* Display a message */ class hello { public static void main(String[] args){ System.out.println("Hello World!"); } }

62 [2-62] Programming in Java Preprocessor Directives Static: indicates that this function remains in memory throughout the execution of the application /* Display a message */ class hello { public static void main(String[] args){ System.out.println("Hello World!"); } }

63 [2-63] Programming in Java Preprocessor Directives Void: indicates that this function does not return a value to the object that calls it /* Display a message */ class hello { public static void main(String[] args){ System.out.println("Hello World!"); } }

64 [2-64] Programming in Java Preprocessor Directives args :can be used in the main function to pass parameters from the operating system command line /* Display a message */ class hello { public static void main(String[] args){ System.out.println("Hello World!"); } }

65 [2-65] Programming in Java Comments Comments are the most important part of your program Criteria for good comments /* Display a message */ class hello { public static void main(String[] args){ System.out.println("Hello World!"); } }

66 [2-66] Programming in Java Java Applet Graphics Interface I/O (1) import java.applet.*; import java.awt.*; import java.awt.event.*; public class AppletInOut extends Applet implements ActionListener { Label prompt; TextField input,output;

67 [2-67] Programming in Java Java Applet Graphics Interface I/O (2) public void init() { prompt = new Label(" 请输入您的名字: "); input = new TextField(6); output = new TextField(20); add(prompt); add(input); add(output); input.addActionListener(this); } public void actionPerformed(ActionEvent e) { output.setText(input.getText()+" ,欢迎你 "); }

68 [2-68] Programming in Java Java Applet Graphics Interface I/O (3)

69 [2-69] Programming in Java Java Application Graphics Interface I/O (1) import java.awt.*; //AWT Package import java.awt.event.*; public class ApplicationGraphicsInOut { public static void main(String args[]) { new FrameInOut(); } } class FrameInOut extends Frame implements ActionListener { Label prompt; TextField input,output; Button btn;

70 [2-70] Programming in Java Java Application Graphics Interface I/O (2) FrameInOut() { super(" 图形界面的 Java Application 程序 "); prompt = new Label(" 请输入您的名字: "); input = new TextField(6); output = new TextField(20); btn = new Button(" 关闭 "); setLayout(new FlowLayout()); add(prompt); add(input); add(output);add(btn); input.addActionListener(this); btn.addActionListener(this); setSize(300,200); show(); }

71 [2-71] Programming in Java Java Application Graphics Interface I/O (3) public void actionPerformed(ActionEvent e) { if (e.getSource() == input) output.setText(input.getText()+" ,欢迎你 "); else { dispose(); System.exit(0); } } }

72 [2-72] Programming in Java Java Applet Graphics Interface I/O (1) import javax.swing.JOptionPane; //Swing Package public class Addition { public static void main( String args[]) { String firstNumber; String secondNumber; int number1,number2,sum; firstNumber = JOptionPane.showInputDialog("Enter first integer"); secondNumber = JOptionPane.showInputDialog("Enter second integer");

73 [2-73] Programming in Java Java Applet Graphics Interface I/O (2) number1 = Integer.parseInt(firstNumber); number2 = Integer.parseInt(secondNumber); sum = number1 + number2; JOptionPane.showMessageDialog(null,"The sum is " + sum, "Results", JOptionPane.PLAIN_MESSAGE); System.exit(0); } }

74 [2-74] Programming in Java Character Interface I/O ( 1 ) import java.io.*; //Input a Character public class ApplicationCharInOut { public static void main(String args[]) { char c = ' ' ; System.out.print("Enter a character please:"); try { c = (char)System.in.read(); }catch(IOException e) {}; System.out.println("You've entered character --- " + c ); }

75 [2-75] Programming in Java Character Interface I/O ( 2 ) import java.io.*; //Input a String public class ApplicationLineIn { public static void main(String args[]) { String s = ""; System.out.print("Please enter a string:"); try { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); s = in.readLine(); }catch(IOException e) {}; System.out.println("You've entered string: " + s ); } }

76 [2-76] Programming in Java Very Familiar with C++ Much of the syntax of the Java language is borrowed from C++ –Variable declarations, assignment statements, and control structures are essentially identical to those of C++ Primitive data types are also recognizable –char, byte, long, float, int, double are joined by the type boolean Comments same as C++

77 [2-77] Programming in Java Differences No “go to” statement. No (explicit) pointers! Automatic garbage collection. No multiple inheritance. Everything in Java is defined within classes. No header files!

78 [2-78] Programming in Java (1) Package and Class Seeking 调包找类 (2) Relationship Declaration 声明关系 (3) Interface Implementation 实现接口 (4) Flexible Use of Methods 巧用方法 (5) Thread Control 线程控制 (6) Exception Handling 异常处理 (7) Network & Communication 网络通信 (8) GUIDesign 精制界面 Key To Learning

79 [2-79] Programming in Java Java is an extension of HTML Programming lang. vs. Page-description lang Java will become a universal programming language for all platforms(lib not powerful enough) Java is interpreted, so it is too slow for serious applications on a specific platform All Java programs run inside a web page Java is an easy programming language to learn … Javascript is one of the Java programsJavascript Common Misconceptions

80 [2-80] Programming in Java JavaScript Example 返回

81 [2-81] Programming in Java Assignment 复习教材第 2 、 3 章 思考题 1. 安装 JDK 2. 在 DOS 环境下编辑、编译、运行讲义中的 application 和 applet 程序 3. 安装 Java 文档 4. 参考 Java 文档,理解实例中的类及其方法 5. 参考实例,编写一个 application ,接收用户输 入的一行字符串,在三行重复输出

82 [2-82] Programming in Java 附录:实习环境 -JDK JDK 目录结构:(以 j2sdk1.4.0 为例)  安装文件:软件 j2sdk-1_4_0-win.exe 文档 j2sdk-1_4_0-doc.zip  根目录:版权、许可、 README 文件、 src.jar ( Java 核心 API 的所有类的源文件)  bin 目录: JDK 开发工具的可执行文件  lib 目录 :开发工具使用的归档包文件  jre : Java 运行时环境的根目录  demo :含有源代码的程序示例  include :包含 C 语言头文件,支持 Java 本地 接口和 Java 虚拟机调试程序接口的本地代码 编程技术

83 [2-83] Programming in Java Java API 文档 以包为目录形式存储,包中包含了对所有类信 息的超链接,文本是 HTML 格式文件,提供了 有关 API 的信息链接 一个类文档包括类层次、类描述、一组成员变 量、类变量构造函数、成员方法、类方法 文档布局是等级制的,主页列出所有的包为超 链接

84 [2-84] Programming in Java 开发环境设置 (1) 假设 JDK 安装在 c:\j2sdk4 目录下 平台为 Windows 2000 、 Windows NT 或 Windows XP 时,需修改环境变量,即在环境变量中增加 classpath ,并修改原有的 path 。 设置方法为依次进入 “ 控制面板 ” | “ 系统 ” | “ 高级 ” | “ 环 境变量 ” ,在系统变量处找到 path ,单击 “ 编辑 ” , 将 c:\j2sdk4\bin 加在后面;再单击 “ 新建 ” ,在加入 新的环境变量 classpath , classpath =.; c:\j2sdk4\lib 单击 “ 确定 ” 按钮后设置生效

85 [2-85] Programming in Java 开发环境设置 (2) 平台为 Windows 9X 时,需修改系统根目录下的 autoexec.bat 文件的 path 和 classpath 变量, 例如: set Path = %path%; c:\j2sdk4\bin set classpath =.; c:\j2sdk4\lib 设置完后,在 DOS 窗口下,敲入 Java 或 Javac 回车后,如果出现其用法参数提示信息,则安 装正确 如果有问题,请检查路径是否正确

86 [2-86] Programming in Java Jecreator 安装 下载 Jecreator 在 JDK 安装后,开始安装 在配置路径时,选择 JDK 安装的目录


Download ppt "[2-1] Programming in Java Introduction to Java and Java Program Lecture2."

Similar presentations


Ads by Google