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:
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) {
...
}
Java programs are portable across operating systems and hardware environments. Portability is to your advantage because:
Three features make Java programs portable:
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:
Browser level (applies to applets only):
The Java language is robust. It has several features designed to avoid crashes during program execution, including:
See Java Basics page 18.
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!!!!
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.
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.
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