Com S 342 meeting -*- Outline -*- * Java overview I use this fairly late in the semester of Fall 1999; it probably should come earlier in the semester... ** declarations *** declaration attributes (modifiers) --------------------------------------------------------- MODIFIERS fields methods classes static = 1 copy a procedure (not part of object) final = constant no overrides no subclasses abstract = not implemented can't instantiate VISIBILITY/PRIVACY MODIFIERS public = visible to all protected = visible to subclasses private = not visible outside this class defaults for declarations within a: class = visible to classes in the same package interface = visible to all --------------------------------------------------------- Q: Why is the default different for interfaces? *** class declarations The syntax discussed below is simplified... --------------------------------------------------------- ::= [] class extends implements {, }* { {}* } --------------------------------------------------------- Q: What should a be? ::= | ::= [] ; | [] () [throws {,}*] ::= ; | ::= `{' {}* `}' ::= [] () [throws {,}*] ::= `{' [] [{}*] `}' Q: How to share code between constructors? Q: How to invoke super's constructor? ::= this ( [] ) ; | super ( [] ) ; *** interface declarations Q: How should an interface differ from a class declaration? - no implements clause - no constructors can be declared - no method implementations *** packages - are naming contexts java.util.Vector vs. myPackage.Vector note: no :: operator for naming as in C++, use "." instead: myPackage.Vector.staticMethod() - contains a set of types (classes and interfaces) and subpackages - corresponds to a directory with the JDK - every class is in a package, but may be the "unnamed package" import P.*; // gets each name, n, in P as shorthand for P.n import Q.m; // just makes m short for Q.m Q: What should be done if the same class is named in 2 imported packages? What are the alternatives? Java forces programmer to use full name ** statements like C++ but: - no go to - labeled break and continue statements - finally clause in the "try" statement --------------------------------------------------------- LABELED STATEMENTS ::= : | break [ ] ; | continue [ ] ; | | ... Example: outer: for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { if (found(a,i,j)) { num_found++; continue outer; } if (bad(a,i,j)) { break; } } } --------------------------------------------------------- --------------------------------------------------------- EXCEPTION HANDLING public class Rat { public Rat(int n, int d) throws ZeroDenomException { if (d == 0) { throw new ZeroDenomException(); } else { /* ... */ } } // ... } public class ZeroDenomException extends Exception {} public class Tester { public void main(String argv[]) { Rat myRat; FileInputStream myFile = newFileInputStream(...); try { int d = ...; myRat = new Rat(3,d); } catch (IOException e) { System.err.println("I/O problem" + e); System.exit(1); } catch (ZeroDenomException e) { System.err.println("hey, no zero denominators!"); myRat = new Rat(1,0); } finally { myFile.close(); } //... } Syntax: ::= try [] ::= catch () [ ] ::= finally --------------------------------------------------------- --------------------------------------------------------- CHECKED AND UNCHECKED EXCEPTIONS Throwable / \ Exception Error / | \ / | ... RuntimeException IOException unchecked exceptions inherit from RuntimeException or Error all others are checked --------------------------------------------------------- ** expressions like C++, but: - no pointers Q: What changes does not having pointers make in the expression grammar? no * and -> operators - no multiple superclasses so no :: name resolution, use "super" instead ** documentation comments /** ... */ can go in front of class or method first line is short form of description tags: @return, @param, @author, @see, @deprecated ** model of objects --------------------------------------------------------- Java vs. C++ Point p; p = new Point(3,4); p = p1; Point[] a; a = new Point[7]; a[0] = new Point(3,4); a[1] = p; --------------------------------------------------------- fill in the equivalent C++ code: Point * p; ... Point *a; ... draw pictures of execution Q: What does assignment do? ** built in facilities *** Object public boolean equals(Object obj); // by default, this does the same as ==, comparing object identity protected Object clone() throws CloneNotSupportedException; Q: What's the point of having clone be protected? can be used to implement clone if you want to make it public One way to do that, implement the Cloneable interface... public Class getClass(); public String toString(); Q: When is toString called? whenever a string is needed, so strings can't produce type errors.