Java Key Points @(http://www.cs.usfca.edu/~parrt, Terence Parr) #### Whence Java? %image(java.progenitors.tiff) #### Program structure A file containing Java source code is considered a compilation unit. Such a compilation unit contains a set of classes and, optionally, a package definition to group related classes together. Classes contain data and method members that specify the state and behavior of the objects in your program. There will be many Java files in your program, one for each class. Java programs come in two flavors: 1 Standalone applications that have no initial context such as a pre-existing main window 1 Applets for WWW programming Execution begins when you launch the {java} virtual machine and tell it to find a particular class. After finding the class, java executes the enclosed {main()} method with signature: << public static void main(String[] args) { ... } >> #### Program execution %image(java-execution.gif) #### Why I like Java ### Portability Java programs are portable across operating systems and hardware environments. Portability is to your advantage because: o You need only one version of your software to serve a broad market. o The Internet, in effect, becomes one giant, dynamic library. o You are no longer limited by your particular computer platform. Three features make Java programs portable: 1 The language. The Java language is completely specified; all data-type sizes and formats are defined as part of the language. By contrast, C/C++ leaves these "details" up to the compiler implementor, and many C/C++ programs therefore are not portable. 1 The library. The Java class library is available on any machine with a Java runtime system, because a portable program is of no use if you cannot use the same class library on every platform. Window-manager function calls in a Mac application written in C/C++, for example, do not port well to a PC. 1 The byte code. The Java runtime system does not compile your source code directly into machine language, an inflexible and nonportable representation of your program. Instead, Java programs are translated into machine-independent byte code. The byte code is easily interpreted and therefore can be executed on any platform having a Java runtime system. (The latest versions of the Netscape Navigator browser, for example, can run applets on virtually any platform). ### Security The Java language is secure in that it is very difficult to write incorrect code or viruses that can corrupt/steal your data, or harm hardware such as hard disks. There are two main lines of defense: Interpreter level: o No pointer arithmetic o Garbage collection o Array bounds checking o No illegal data conversions Browser level (applies to applets only): o No local file I/O o Sockets back to host only o No calls to native methods ### Robustness The Java language is robust. It has several features designed to avoid crashes during program execution, including: o No pointer arithmetic o Garbage collection--no bad addresses o Array and string bounds checking o No jumping to bad method addresses o Interfaces and exceptions #### Assignment semantics See @(http://www.cs.usfca.edu/~parrt/doc/java/JavaBasics-notes.pdf, Java Basics) page 18. #### Equality Two Java primitive types are equal (using the == operator) when they have the same value (e.g., {3 == 3}). However, two object variables are equal if and only if they refer to the same instantiated object--a "shallow" comparison. For example, << void test() { Data a = new Data(1); Data b = new Data(2); Data c = new Data(1); // a == b is FALSE // a == c is FALSE (in C++, this'd be TRUE) Data d = a; Data e = a; // d == e is TRUE, // d,e are referring to same object } >> To perform a "deep" comparison, the convention is to define a method called {equals()}. You would rewrite {Data} as: << class Data { public int data = 0; public Data(int d) { data = d; } boolean equals(Object o) { Data d = (Data)o; return data == d.data; } } ... Data a = new Data(1); Data b = new Data(1); // a.equals(b) is true!!!! >> #### Garbage collection An automatic garbage collector deallocates memory for objects that are no longer needed by your program, thereby relieving you from the tedious and error-prone task of deallocating your own memory. As a consequence of automatic garbage collection and lack of pointers, a Java object is either null or valid--there is no way to refer to an invalid or stale object (one that has been deallocated). In the Java language, memory leaks are not an issue. The following Java method causes no ill effects: << void f() { T t; for (int i = 1; i <= 1000; i++) { t = new T(); } } >> In Java, each time t is assigned a new reference, the old reference is now available for garbage collection. Note that it isn't immediately freed; it remains allocated until the garbage collector thread is next executed and notices that it can be freed. Put simply, automatic garbage collection reduces programming effort, programming errors, and program complexity. #### Dynamic class loading There is no link phase for Java programs; all linking is done dynamically at runtime. You may think of them as a collection of DLLs (dynamically loadable libraries) that are linked on demand at runtime. #### Package summary Language: java.lang (String, Object, ...) Collections: java.util (List, Collection, Calendar, ...) Databases: java.sql Networking: java.net File I/O: java.io GUI: javax.swing Servlets: javax.servlet