CMSC 433,Michael Hicks, U. Maryland (via W. Pugh, A. Sussman, and A, Porter)1CMSC433, Fall 2002Programming Language Technology and ParadigmsBasic JavaMichael HicksSep 5, 2002CMCS 433, Fall 2002 -Michael Hicks2Administrivia•Project 1 posted today•Java readings from Thinking in Java–we’ll do parts of many chapters, but definitely read Chapter 1 for an overview–I’ll add some suggestions on Web page, but use it as a referenceCMSC 433,Michael Hicks, U. Maryland (via W. Pugh, A. Sussman, and A, Porter)2CMCS 433, Fall 2002 -Michael Hicks3Outline•Object oriented programming principles–How Java realizes them–How Java differs from C++•Useful information on Java (not covered in class)–Using the compiler–I/O libraries–Container classesCMCS 433, Fall 2002 -Michael Hicks4Software Engineering Goals•Reliability (it works!)•Performance•Reusability (write-once, then reuse)•Maintainability–Easy to modify/extend–Easy to understand•Quick development timeCMSC 433,Michael Hicks, U. Maryland (via W. Pugh, A. Sussman, and A, Porter)3CMCS 433, Fall 2002 -Michael Hicks5Object Orientation•Abstraction–focus on essential properties, ignore unimportant details•Encapsulation–separate external, visible behavior from internal, hidden behaviorCMCS 433, Fall 2002 -Michael Hicks6Object Orientation (cont’d)•Combining data and behavior–objects, not developers, decide how to carry out operations•Sharing–similar operations and structures are implemented once•Emphasis on object-structure rather than procedure structure–behavior more stable than implementation–… but procedure structure still usefulCMSC 433,Michael Hicks, U. Maryland (via W. Pugh, A. Sussman, and A, Porter)4CMCS 433, Fall 2002 -Michael Hicks7And one more …•Security and Reliability–Write code that works, and is not insecureCMCS 433, Fall 2002 -Michael Hicks8Java•Similar to C++, but with “unsafe” features removed, and others added•Fully specified, compiles to virtual machine–machine-independent•Secure–bytecodeverification (“type-safe”)–security managerCMSC 433,Michael Hicks, U. Maryland (via W. Pugh, A. Sussman, and A, Porter)5CMCS 433, Fall 2002 -Michael Hicks9Java design•Everything inherits from Object*–Allows sharing, generics, and more*Well, almost: there are primitive int, long, float, etc. ObjectThreadIntegerNumber…CMCS 433, Fall 2002 -Michael Hicks10Java Design•Inheritance–Hierarchical code sharing (“is-a”)•Interfaces–For “mixins” & non-hierarchical frameworksObjectNumberinherits fromComparablepublic int compareTo(Object o)implementsCMSC 433,Michael Hicks, U. Maryland (via W. Pugh, A. Sussman, and A, Porter)6CMCS 433, Fall 2002 -Michael Hicks11Java Design•Security and reliability–Strong type system•Object o = (Object)27;not allowed!–Garbage collection•No free()–Exceptions•Separation of error-handling from algorithmCMCS 433, Fall 2002 -Michael Hicks12Java libraries and features• Utilities–collection classes, Zip files, internationalization•GUIs, graphics and media•Networking–sockets, URLs, RMI, CORBA•Threads•Databases•Cryptography/securityCMSC 433,Michael Hicks, U. Maryland (via W. Pugh, A. Sussman, and A, Porter)7CMCS 433, Fall 2002 -Michael Hicks13Some of what’s missing from C++•Preprocessor (#include, #define, …)• Some “low-level” types–structsand unions–enumerated types–bit-fields•Some function features–variable-length argument lists–operator overloading•Some class features–multiple inheritance (of implementation)–templates/ parameterized types (but now in 1.4!)CMCS 433, Fall 2002 -Michael Hicks14Naming conventions•Classes/Interfaces start with a capital letter–Object, Number, Thread, …•packages/methods/variables start lowercase–ThreadmyThread = new Thread();–java.lang, org.xml.sax•Capitalize multi-word names (no underscores)–SortedList, compareTo, toBinaryString•CONSTANTS all in uppercase (use underscores)–PI, E, MAX_VALUECMSC 433,Michael Hicks, U. Maryland (via W. Pugh, A. Sussman, and A, Porter)8Object-oriented programming in JavaCMCS 433, Fall 2002 -Michael Hicks16Java Classes and Objects• Each object is an instance of a class–an array is an object•Each class extends onesuperclass–Objectif not specified–class Objecthas no superclassCMSC 433,Michael Hicks, U. Maryland (via W. Pugh, A. Sussman, and A, Porter)9CMCS 433, Fall 2002 -Michael Hicks17Objects have methods•All objects, therefore, inherit them–Default implementations may not be the ones you wantpublic booleanequals(Object that)public StringtoString()public int hashCode()public void finalize()–And others …“conceptual” equalityreturns print representationkey for hash tablecalled when object garbage-collectedCMCS 433, Fall 2002 -Michael Hicks18Objects and references•All objects allocated on the heap with new()–All variables of non-primitive type are references to an object or null; assignment (=) copies references–No object can “contain” another object– No objects stack-allocated (only references there)•Reference is like a C++ pointer, except–can only point to start of heap-allocated object–no pointer arithmetic allowed–use . instead of -> to access fields/methodsCMSC 433,Michael Hicks, U. Maryland (via W. Pugh, A. Sussman, and A, Porter)10CMCS 433, Fall 2002 -Michael Hicks19String exampleCMCS 433, Fall 2002 -Michael Hicks20Equality•Object.equals() method–Structural (“conceptual”) equality•== operator–true if arguments referencethe same object–o == p →o.equals(p)CMSC 433,Michael Hicks, U. Maryland (via W. Pugh, A. Sussman, and A, Porter)11CMCS 433, Fall 2002 -Michael Hicks21class Complex – a toy examplepublic class Complex {private double r, i;public Complex(double r,double i) {this.r = r;this.i = i;}public String toString() {return “(“ + r + “,” + i + “)”;}public Complex plus(Complex that) {return new Complex(r + that.r,i + that.i);}}CMCS 433, Fall 2002 -Michael Hicks22Using Complexpublic static void main(String[] args) {Complex a = new Complex(5.5,9.2);Complex b = new Complex(2.3,-5.1);Complex c,d;c = a.plus(b);d = a.plus(b);System.out.println(“a = “ + a);System.out.println(“b = “ + b);System.out.println(“c = “ + c);System.out.println(“c.equals(d): “ + (c.equals(d)));System.out.println(“c = d: “+(c==d));}prints:a = (5.5,9.2)b = (2.3,-5.1)c = (7.8,4.1) c.equals(d): falsec == d: falseCMSC 433,Michael Hicks,
View Full Document