600 likes | 914 Views
Java Programming. 郝聚涛 haojutao@163.com. teaching material. Danny Poo.Java Programming 清华大学出版社 ,2010. Reference Books. (美)梁( Liang , Y.D. ) Java 语言程序设计(基础篇)(英文版 / 第 6 版), 2008 Bruce Eckel , Thinking in Java, Third Edition , Prentice Hall PTR , 2003. contents.
E N D
Java Programming 郝聚涛 haojutao@163.com 上海理工大学 Java Programming
teaching material • Danny Poo.Java Programming 清华大学出版社,2010 上海理工大学 Java Programming
Reference Books • (美)梁(Liang,Y.D.) Java语言程序设计(基础篇)(英文版/第6版),2008 • Bruce Eckel,Thinking in Java, Third Edition,Prentice Hall PTR ,2003 上海理工大学 Java Programming
contents • Chapter 1 :the java programming environment • Chapter 2 :The java language components • Chapter 3:Expression,Statemments and Operators • Chapter 4:Program Flow Control • Chapter 5:Arrays • Chapter 6:Methods • Chapter 7:Class and Objects • Chapter 8:The Java Application Programming Interface(API) 上海理工大学 Java Programming
supplementary content • GUI Graphical User Interface • Applet /Servlet • MultiThread programming 上海理工大学 Java Programming
The java Programming Environment • Java is a programming language • Java is an object-oriented programming language • Java is the Internet programming language. 上海理工大学 Java Programming
Popular High-Level Languages • 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, based on C) • C# (C sharp, a Java-like language developed by Microsoft) • Java (We use it in the book) Delphi /C#之父-Anders Hejlsberg (安德斯·海尔斯伯格 ) Turbo Pascal、VisualJ++、Delphi、C#的缔造者,.NET的领军人物,Borland的创始人之一,Microsoft的灵魂人物。 上海理工大学 Java Programming
Java’s History • James Gosling(詹姆斯·戈士林)----Java 技术之父 and Sun Microsystems • Sun“绿色计划”----- Oak(橡树 ) • Java, May 20, 1995, Sun World • HotJava • The first Java-enabled Web browser 上海理工大学 Java Programming
52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 Ada Modula-2 Pascal ALGOL60 ALGOL68 C BCPL CPL B C++ Smalltalk 80 Java Simula 67 QBASIC VB ANSI-BASIC BASIC FORTRAN90 FORTRAN FORTRAN77 PL/1 COBOL LISP PROLOG 上海理工大学 Java Programming
程序设计语言选择 影响语言发展和前途的主要有两个的因素 • 语言和专业的领域有很大的关系 • c/c++语言在操作系统,图形处理方面、工业控制、嵌入式系统的优势,肯定大于java; • c#、java在internet上的应用肯定强于c++。 • 语言与操作系统有很大关系 • C#根植windows • java服务器大多运行linux/unix上。 上海理工大学 Java Programming
语言: c/c++、 c#、 java • 操作系统:windows、 linux/unix; • 应用领域:传统应用, 桌面程序, 互联网; • 传统应用包括:驱动开发、图形处理、工业控制、底层通信协议、嵌入式系统 • 桌面程序:主要指有界面个人的应用程序 • 从领域的影响力和应用的规模上来看应该是: 桌面程序 < 传统应用 < 互联网; 上海理工大学 Java Programming
Why Java? The answer is that Java enables users to develop and deploy applications on the Internet for servers, desktop computers, and small hand-held devices.. • Java is a general purpose programming language. • Java is the Internet programming language. 上海理工大学 Java Programming
Java, Web, and Beyond • Java can be used to develop Web applications. • Java Applets • Java can also be used to develop applications for hand-held devices such as Palm and cell phones 上海理工大学 Java Programming
Examples of Java’s Versatility (Applets) 上海理工大学 Java Programming
<html> <head> <title> HTML Test Page </title> </head> <body> LoanApp will appear below in a Java enabled browser.<br> <applet codebase = "." code = "LoanApp.class" name = "TestApplet" width = "400" height = "300" hspace = "0" vspace = "0" align = "middle" > </applet> </body> </html> 上海理工大学 Java Programming
Examples of Java’s Versatility (Applets) 上海理工大学 Java Programming
Examples of Java’s Versatility(Web Server Applications) 上海理工大学 Java Programming
JSP <html> <body> <H1>The time in second is: <%= System.currentTimeMillis()/1000 %> </H1> </body> </html> 上海理工大学 Java Programming
1970年1月1日0时0分0秒起至现在的总秒数 UNIX时间,从从格林威治时间1970年1月1日0时0分0秒起至现在的总秒数,不包括闰秒。 上海理工大学 Java Programming
请求 响应 执行 3 客户端 转译 1 编译 2 Servlet JSP 执行过程 JSP 页面 JSP 执行过程 上海理工大学 Java Programming
PDA and Cell Phone 上海理工大学 Java Programming
There were five primary goals in the creation of the Java language: • It should be "simple, object oriented, and familiar". • It should be "robust and secure". • It should be "architecture neutral and portable". • It should execute with "high performance". • It should be "interpreted, threaded, and dynamic". 上海理工大学 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 Programming
A Simple java Program HelloWorld.java 上海理工大学 Java Programming
Compiling Source Code • A program written in a high-level language is called a source program. • a program called a compiler is used to translate the source program into a machine language program called an object program. • The object program is often then linked with other supporting library code before the object can be executed on the machine. Source File Compiler Object File Executable File Linker 上海理工大学 Java Programming
Compiling Java Source Code • You can port a source program to any machine with appropriate compilers. The source program must be recompiled. • Java was designed to run object programs on any platform. • With Java, you write the program once, and compile the source program into a special type of object code, known as bytecode. • Java Virtual Machine is a software that interprets Java bytecode. 上海理工大学 Java Programming
JDK Versions • JDK 1.02 (1995) • JDK 1.1 (1996) • JDK 1.2 (1998) java 2 • JDK 1.3 (2000) • JDK 1.4 (2002) • JDK 1.5 (2004) a. k. a. JDK 5 or Java 5 • JDK 1.6 (2006) a. k. a. JDK 6 or Java 6 上海理工大学 Java Programming
JDK Editions • Java Standard Edition (J2SE) • J2SE can be used to develop client-side standalone applications or applets. • Java Enterprise Edition (J2EE) • J2EE can be used to develop server-side applications such as Java servlets and Java ServerPages. • Java Micro Edition (J2ME). • J2ME can be used to develop applications for mobile devices such as cell phones. This book uses J2SE to introduce Java programming. 上海理工大学 Java Programming
Popular Java IDEs • NetBeans Open Source by Sun • Eclipse Open Source by IBM • Borland JBuilder (Based on Eclipse) 上海理工大学 Java Programming
Installing and Configuring JDK 1.6 • Downloading JDK 1.6 • http://java.sun.com/javase/downloads/index.jsp. • • Installing JDK 1.6 • • Configuring JDK • • Verifying Configurations 上海理工大学 Java Programming
Installing JDK 1.6 J2SE——J2SDK的核心部分 • 开发工具 • 编译器 • 调试器 • 文档制作工具 • 运行环境 • Java 虚拟机 • 组成Java 2 平台API的类。 • 帮助文档 • 附加库 • Java程序(Applets 和 Applications) 的演示 …… 上海理工大学 Java Programming
Java开发环境(续) Java开发工具包括 • Javac: • Java编译器,用来将java程序编译成 Bytecode。 • Java: • Java解释器,执行已经转换成Bytecode的java应用程序。 • Jdb: • Java调试器, 用来调试java程序。 • Javap: • 反编译,将类文件还原回方法和变量。 • Javadoc: • 文档生成器,创建HTML文件。 • Appletviwer: • Applet解释器, 用来解释已经转换成Bytecode的java小应用程序。 上海理工大学 Java Programming
Java开发环境(续) 安装JDK后产生如下目录: • \bin目录:Java开发工具,包括Java编译器、解释器等 • \demo目录:一些实例程序 • \lib目录:Java开发类库 • \jre目录: Java运行环境,包括Java虚拟机、运行类库等 • … 上海理工大学 Java Programming
Configuring JDK 1.6 on Windows • Configuring JDK on Windows NT, 2000, XP, and ME • Right-click the My Computer icon on your desktop, open the System Properties window 上海理工大学 Java Programming
Verifying Configurations 上海理工大学 Java Programming
Anatomy(剖析) of a Java Program • Comments 注释 • Package 包 • Reserved words 保留字关键字 • Modifiers 修饰符 • Statements 语句 • Blocks 块 • Classes 类 • Methods 方法 • The main method 上海理工大学 Java Programming
Comments • Three types of comments in Java. • Line comment: • A line comment is preceded by two slashes (//) in a line. • Paragraph comment: • A paragraph comment is enclosed between /* and */ in one or multiple lines. • javadoc comment: • javadoc comments begin with /**and end with */. They are used for documenting classes, data, and methods. They can be extracted into an HTML file using JDK's javadoc command. 上海理工大学 Java Programming
javadoc DocTest.java • 例如:DocTest.java文件: /** 这是一个文档注释的例子,主要介绍这个类 */public class DocTest{ /** 变量注释,下面这个变量主要是充当整数计数 */public int i;/** 方法注释,下面这个方法的主要功能是计数 */ public void count( ) {}} html 上海理工大学 Java Programming
Package • The second line in the program (package chapter1;) specifies a package name, chapter1, for the class Welcome. package chapter1 //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } • Package (姓).class (名) chapter1.Welcome • The Java compiler • compiles the source code in Welcome.java • generates Welcome.class • stores Welcome.class in the chapter1 folder. 上海理工大学 Java Programming
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 Listing 1.1 are • public • static • void • Their use will be introduced later in the book. 上海理工大学 Java Programming
Modifiers • Java uses certain reserved words called modifiers that specify the properties of the data, methods, and classes and how they can be used. Examples of modifiers • public • static • Other modifiers • private • final • abstract • protected • A public datum, method, or class can be accessed by other programs. • A private datum or method cannot be accessed by other programs. • Modifiers are discussed in Chapter 6, “Objects and Classes.” 上海理工大学 Java Programming
Statements • A statement represents an action or a sequence of actions. • The statement System.out.println("Welcome to Java!") in the program in Listing 1.1 is a statement to display the greeting "Welcome to Java!" Every statement in Java ends with a semicolon (;) 上海理工大学 Java Programming
Blocks A pair of braces in a program forms a block that groups components of a program. 上海理工大学 Java Programming
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. 上海理工大学 Java Programming
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. 上海理工大学 Java Programming
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; } 上海理工大学 Java Programming
Java源文件的命名规则 • 如果在源程序中包含有公共类的定义,则该源文件名必须与该公共类的名字完全一致,字母的大小写都必须一样。 • 这是java语言的一个严格的规定,如果不遵守,在编译时就会出错。 • 因此,在一个java源程序中至多只能有一个公共类的定义 • 如果源程序中不包含公共类的定义,则该文件名可以任意取名 • 如果在一个源程序中有多个类定义,则在编译时将为每个类生成一个.class文件 上海理工大学 Java Programming
Programming Style and Documentation • Programming style deals with what programs look like. • Documentation is the body of explanatory remarks and comments pertaining to a program. • Good programming style and appropriate documentation reduce the chance of errors and make programs easy to read. • Appropriate Comments • Naming Conventions • Proper Indentation and Spacing Lines • Block Styles 上海理工大学 Java Programming
Appropriate Comments Include a summary at the beginning of the program to explain what the program does, its key features, its supporting data structures, and any unique techniques it uses. Include your name, due date, date submitted, program name and a brief description at the beginning of the program. /**************************************** * * Student Name: * Date Due: * Date Submitted: * Program Name: * Program Description: * ****************************************/ 上海理工大学 Java Programming