DOC PREVIEW
UMBC CMSC 331 - Introduction to JAVA

This preview shows page 1-2-3-4-5-36-37-38-39-40-72-73-74-75-76 out of 76 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 76 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 76 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 76 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 76 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 76 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 76 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 76 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 76 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 76 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 76 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 76 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 76 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 76 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 76 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 76 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 76 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Introduction to JAVAIntroductionFeatures of JavaBuilding Standalone JAVA Programs (on UNIX)Java Virtual MachineHelloWorld (standalone)AppletsBuilding AppletsSlide 9hello.htmlData Types in JavaArray AllocationArray OperationsEcho.javaJAVA ClassesSyntax NotesExample: FIFOSlide 18Slide 19Slide 20Slide 21Slide 22Programming by ContractSlide 24Java vs. C++Single Inheritance, butPackagesClasses and InterfacesVisibility of MethodsApplets vs. Stand-aloneExceptionstry/catch/finallyA Tour of the Java APIThe Java APIThe package java.langObserving an object’s classStrings in JavaStringBuffers in JavaJava.lang.systemThe Cloneable InterfaceThe class java.utilSystem PropertiesSlide 43Slide 44Java GUIGUI ExamplesSlide 47Slide 48ScribbleSlide 50Slide 51Slide 52Slide 53Java.netManipulating URLsSlide 56Slide 57UDP ExamplesSlide 59Slide 60Slide 61Slide 62Using ThreadsThe Runnable InterfaceUsing a Single ThreadSlide 66Slide 67Neon Sign ExampleSlide 69Slide 70Multiple ThreadsMultiple Threaded ExampleSlide 73Slide 74Other IdeasWhere to get more informationIntroduction to JAVACMSC 331Spring 1999Introduction •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 SGIFeatures of Java•Object-oriented programming language–clean, simple syntax–similar to C, but without complexity of C++•Comprehensive API (application program interface)–platform independence–useful classes and methodsBuilding Standalone JAVA Programs (on UNIX)•Prepare the file foo.java using an editor•Invoke the compiler: javac foo.java•This creates foo.class•Run the java interpreter: java fooJava 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 have done this, e.g. UCSD Pascal•This approach provides platform independence, and greater securityHelloWorld (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 classApplets•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 thisBuilding Applets•Prepare the file foo.java, and compile it to create foo.class•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 MachineHelloWorld.javaimport java.applet.*;public class HelloWorld extends Applet { public void init() { System.out.println("Hello, world!"); }}hello.html<title>Hello, World</title><h1>Hello, World</h1><applet code="HelloWorld.class" width=100 height=140></applet>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 754Array Allocation•Declared in two ways:–float Vector1[] = new float[500];–int Vector2[] = {10, 20, 30, 40};•Not allocated on stack, but dynamically•Are subject to garbage collection when no more references remain–so fewer memory leaks–Java doesn’t have pointers!Array Operations•Subscripts start at 0 as in C•Subscript checking is done automatically•Certain operations are defined on arrays of objects, as for other classes–e.g. Vector1.length == 500Echo.javaC:\UMBC\331\java>type echo.java// This is the Echo example from the Sun tutorialclass echo { public static void main(String args[]) { for (int i=0; i < args.length; i++) { System.out.println( args[i] ); } }}C:\UMBC\331\java>javac echo.javaC:\UMBC\331\java>java echo this is pretty sillythisisprettysillyC:\UMBC\331\java>JAVA 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 classSyntax Notes•No global variables–class variables and methods may be applied 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 languagesExample: FIFO•To show how simple data structures are built without pointers, we’ll build a doubly-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 addedpublic class ListItem { // In file ListItem.java public Object x; // N.B. a heterogeneous queue public ListItem previous; public ListItem next; // Constructor operation takes initial value public ListItem(String val) { // this refers to “current” object this.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 objects return ( x.equals(c.x) && (previous == c.previous) && (next == c.next) ); } public void printItem() { System.out.println(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 removed public ListItem last = null; // last is the item most recently added // Called to initialize and test the applet. More detail on next page. public void init() { System.out.println("isEmpty returns "+isEmpty()); putQueue("node 1"); ... getQueue().printItem(); ... } // See if the queue is empty public boolean isEmpty() { ... } // Add an item to the queue public void putQueue(String value) { ... } // Get the first item off the front of the queue public 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.out.println("First node is


View Full Document

UMBC CMSC 331 - Introduction to JAVA

Documents in this Course
Semantics

Semantics

14 pages

Java

Java

12 pages

Java

Java

31 pages

V

V

46 pages

Semantics

Semantics

11 pages

Load more
Download Introduction to JAVA
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Introduction to JAVA and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Introduction to JAVA 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?