Introduction to JAVA.ppt
《Introduction to JAVA.ppt》由会员分享,可在线阅读,更多相关《Introduction to JAVA.ppt(76页珍藏版)》请在麦多课文档分享上搜索。
1、Introduction to JAVA,CMSC 331,Spring 1999,Introduction,Present the syntax of Java Introduce the Java API Demonstrate how to build stand-alone Java programs Java applets, which run within browsers e.g. Netscape Example programs tested using Java on Windows 98 and/or SGI,Features of Java,Object-orient
2、ed programming language clean, simple syntax similar to C, but without complexity of C+ Comprehensive API (application program interface) platform independence useful classes and methods,Building Standalone JAVA Programs (on UNIX),Prepare the file foo.java using an editor Invoke the compiler: javac
3、foo.java This creates foo.class Run the java interpreter: java foo,Java Virtual Machine,The .class files generated by the compiler are not executable binaries so Java combines compilation and interpretation Instead, they contain “byte-codes” to be executed by the Java Virtual Machine other languages
4、 have done this, e.g. UCSD Pascal This approach provides platform independence, and greater security,HelloWorld (standalone),public class HelloWorld public static void main(String args) System.out.println(“Hello World!“); ,Note that String is built in println is a member function for the System.out
5、class,Applets,The JAVA virutal machine may be executed under the auspices of some other program, e.g. a Web browser. Bytecodes can be loaded off the Web, and then executed locally. There are classes in Java to support this,Building Applets,Prepare the file foo.java, and compile it to create foo.clas
6、s Invoke an Applet Viewer, or a Java-aware browser such as Netscape, and open an HTML file such as foo.html Browser invokes the Java Virtual Machine,HelloWorld.java,import java.applet.*;public class HelloWorld extends Applet public void init() System.out.println(“Hello, world!“); ,hello.html,Hello,
7、World Hello, World,Data Types in Java,Primitive data types similar to C boolean true and false char 16 bits UNICODE byte, short, int, long integers; 8, 16, 32 and 64 bits respectively float and double IEEE 754,Array Allocation,Declared in two ways: float Vector1 = new float500; int Vector2 = 10, 20,
8、 30, 40; Not allocated on stack, but dynamically Are subject to garbage collection when no more references remain so fewer memory leaks Java doesnt have pointers!,Array Operations,Subscripts start at 0 as in C Subscript checking is done automatically Certain operations are defined on arrays of objec
9、ts, as for other classes e.g. Vector1.length = 500,Echo.java,C:UMBC331javatype echo.java / This is the Echo example from the Sun tutorial class echo public static void main(String args) for (int i=0; i javac echo.javaC:UMBC331javajava echo this is pretty silly this is pretty sillyC:UMBC331java,JAVA
10、Classes,The class is the fundamental concept in JAVA (and other OOPLs) A class describes some data object(s), and the operations (or methods) that can be applied to those objects Every object and method in Java belongs to a class,Syntax Notes,No global variables class variables and methods may be ap
11、plied to any instance of an object methods may have local (private?) variables No pointers but complex data objects are “referenced” Other parts of Java are borrowed from PL/I, Modula, and other languages,Example: FIFO,To show how simple data structures are built without pointers, well build a doubl
12、y-linked list ListItem class has some user data first refers to that ListItem object at the front of the queue last refers to the object at the end of the queue, i.e. most recently added,public class ListItem / In file ListItem.javapublic Object x; / N.B. a heterogeneous queuepublic ListItem previou
13、s;public ListItem next;/ Constructor operation takes initial valuepublic ListItem(String val) / this refers to “current” objectthis.x = val;this.previous = this.next = null;public boolean equals(ListItem c) / two ListItems are equal if their string values / are equal and they point to the same objec
14、tsreturn ( x.equals(c.x) ,import java.applet.*; / overview of fifo.javapublic class fifo extends Applet private int count = 0;public ListItem first = null; / first is the next item to be removedpublic ListItem last = null; / last is the item most recently added/ Called to initialize and test the app
15、let. More detail on next page.public void init() System.out.println(“isEmpty returns “+isEmpty();putQueue(“node 1“);.getQueue().printItem();./ See if the queue is emptypublic boolean isEmpty() . / Add an item to the queuepublic void putQueue(String value) . / Get the first item off the front of the
16、queuepublic ListItem getQueue() . ,/ Called to initialize and test the applet.public void init() System.out.println(“isEmpty returns “+isEmpty();putQueue(“node 1“);System.out.println(“First node is “); first.printItem();System.out.println(“Last node is “); last.printItem();putQueue(“node 2“);System.
17、out.println(“First node is “); first.printItem();System.out.println(“Last node is “); last.printItem();getQueue().printItem();System.out.println(“First node is “); first.printItem();System.out.println(“Last node is “); last.printItem();getQueue().printItem();System.out.println(“isEmpty returns “+isE
18、mpty();,/ See if the queue is emptypublic boolean isEmpty() return (count = 0); / Add an item to the queuepublic void putQueue(String value) ListItem newItem = new ListItem(value);if ( isEmpty() ) / Special case of empty queuefirst = last = newItem; else / next is the next item in the queue/ previou
19、s is the item (if any) that was in the / queue right ahead of this (current) itemlast.next = newItem;newItem.previous = last;last = newItem;count+;,/ Get the first item off the front of the queuepublic ListItem getQueue() ListItem firstItem = first;/ Make sure the queue isnt emptyif (isEmpty() ) Sys
20、tem.out.println(“Called getQueue on an empty queue“); else this.first = firstItem.next;/ Did we just remove the only item in the queue?if (first = null) last = null; else first.previous = null;count-;return firstItem;,Programming by Contract,Note that the integer variable count, and first and last (
21、both of type ListItem, are redundant in that first and last are null iff count = 0 first = last , but both not null iff count = 1 otherwise first != last iff count 1 Java has no assert macro, but we can test and throw an exception.,/ See if the queue is empty/ Check consistency of count, first and l
22、ast/ Note that exceptions are first-class objectsclass CorruptFifoException extends Exception;.public boolean isEmpty() if (count = 0) if (first = null else / count != 0.,Java vs. C+,Lots of similarity to C+ expressions (, operator okay only in loops) control structures same, except breaks and conti
23、nues may have labels, e.g. to escape from switch statements Java has single inheritance Java doesnt do templates,Single Inheritance, but,A class may extend only one class, but it may implement many others A subclass inherits the variables and methods of its superclass(es), but may override them Over
24、rides the methods defined in the class(es) it implements, as in upcoming thread example,Packages,Classes may be grouped into packages Six packages come with Java Packages add functionality without extending the language per se The import statement lets you use a method without its package name, e.g.
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- INTRODUCTIONTOJAVAPPT
