DOC PREVIEW
Duke CPS 108 - Java on one slide

This preview shows page 1 out of 2 pages.

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

Unformatted text preview:

Software Design7.1Java on one slidez All objects allocated on heap, via new, garbage collected¾ Primitive types like int, double, boolean exempt• Everything else subclasses Object¾ All variables (non-primitive) are pointers aka references• Can we compare pointers for equality? Is this a problem?z No free functions, everything in a class, inheritance by default¾ Functions and classes can be final, not inheritable from ¾ Static functions like Math.sqrt are like free functions¾ Local variables must be assigned to, instance variables all initialized by default to 0, nullz Containers contain only non-primitive types¾ Conversion between int and Integer can be ugly¾ Use ArrayList and HashMap instead of Vector, HashtableSoftware Design7.2Java on another slidez Public class Foo must be in a file Foo.java¾ Compiled into Java bytecodes, stored in Foo.class• Bytecodes executed inside a JVM: Java Virtual Machine• JVM is architecture specific, often relies on native/C code¾ Helper/non-public classes can be in same file• Keep related/cohesive concepts together• Don’t go overboardz Execution starts with a static main function¾ Any class can have such a function, class invoked specifically via java Foo (runs Foo.main)z The environment is important and essential¾ You need to understand CLASSPATH to leverage JavaSoftware Design7.3From STL to Javaz In STL an iterator is a concept, there are refinements¾ Input, output, forward, bidirectional, random access• A forward iterator is an input iterator and an output iterator• The iterator may be immutable (or const)---read only¾ Refinements not implemented by inheritance, but by design, contract, and subsequently implementation• What happens if you try to implement an STL iterator?z In Java Iterator is an interface (like a base class), similar to Tapestry iterators¾ Collection(s) are required to have iterators, these are used in some operations like max, min, construct vector, …¾ Related to STL as algorithm glue, but very different Software Design7.4WordCount.java, print strings, line #’spublic void print() { Iterator allKeys = myMap.keySet().iterator(); // wordswhile (allKeys.hasNext()) { String key = (String) allKeys.next(); System.out.print(key + "\t");Iterator lines = ((Set) myMap.get(key)).iterator(); while (lines.hasNext()) { System.out.print(lines.next() + " "); } System.out.println(); } }z Differences between Java and Tapestry in practice?¾ Must store current element since next() does two things¾ Must cast since Collections store ObjectsSoftware Design7.5Java inheritancez By default every class can be a base/parent class, every method is polymorphic. To inherit use extends keyword¾ Can change with final keyword (similar to const, but not)¾ A class can extend only one base class (but see interfaces)¾ Public, protected, private similar to C++, what’s not?z A class can be an abstract class, public abstract class Foo¾ Can’t instantiate (no new Foo()), but can extend¾ A method can be abstract, like pure virtual in C++z A class implements any number of interfaces¾ Like ABC, but function prototypes only, no state¾ Subclass must implement all methods of interfaceSoftware Design7.6Modules and Packagesz Java code/modules organized into packages¾ C++ has namespaces, required and now used¾ Java uses packages: corresponds to directory hierarchy¾ We’re using the default package (no name) later we’ll use packages¾ java.util, java.lang, java.io, … are all packagesz The import statement at the beginning of a program doesn’t work like #include, it tells the Java compiler where to look to resolve names¾ Differences from


View Full Document

Duke CPS 108 - Java on one slide

Download Java on one slide
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 Java on one slide 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 Java on one slide 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?