CS 541 Meeting -*- Outline -*- * Other features of Java ** portability ------------------------------------------ PORTABILITY What helps Java code be more easily portable? ------------------------------------------ by minimizing machine dependencies -lots of built in standard libraries -bytecodes and exact semantics for the language properties of types, order of evaluation, etc. -binary compatability (through the JVM, even on diff machines) Q: And how does that compare to C or C++? trades run-time efficiency for it ** initialization all variables must be given a value before use ------------------------------------------ INITIALIZATION All variables void foo(Float f) { Double d; if (...) { d = new Double(7.3 + f); } else { ... } Double d2 = d; } ------------------------------------------ ... must be initialized before first use, and the compiler statically enforces this explain the analogy to type checking here ** final ------------------------------------------ FINAL final double pi = 3.14159; class Foo { public final void int f() { ... } } final class Bar extends Foo { } ------------------------------------------ used like const in C++ on fields in Java more interesting is it's use in preventing subclassing or overriding of methods Q: Why would one want to prevent overriding a method? security, behavioral guarantees efficiency (allow compiler to treat it specially) Q: Why would one want to prevent a class from being subclassed? efficiency (allow compilers to know about its objects) security, behavioral guarantees ** control flow ------------------------------------------ CONTROL FLOW Like C/C++ but: search: { if (k == i) { loop: for (z = 0; z < size; z++) { if (a[z] == j) { break search; } for (y = 0; y < lb; y++) { ... if (a[z] < m && b[y] > q) { continue loop; } ... } ... } } } ------------------------------------------ ... no goto (too bad they didn't fix switch...) adds break continue