last updated 1/20/08
An object has behavior.
An object has state.
An object has a unique identity, even if it has the same state as another object
A class in OOP is a module.
Module - a generalization of a function: a collection of functions, data types, constants and variables packaged together into separately compiled units.
Public view -
the specification of a module and its components that are usable by the user. This is
usually combined with the function prototypes. int F1(); // Function F1 behaves // as follows: //..pre/post.. int F2(); // Function F2 behaves // as follows: //..pre/post.. |
Private view - the
implementation details of a module such as the actual code and local variables.
int F1()
//comments repeated
{
...
}
int F2()
//comments repeated
{
...
}
|
In Java: standard documentation file (JavaDoc) can be generated from a standard form of commenting in the class definition. You then supply the .class file and the Javadoc information for the user.
Encapsulation - the grouping of related items for a module and hiding of the private components from the public view.
Separate Compilation and Linking example for C++.
Linking - the final step in combining all the modules together for a complete program
Java does the linking dynamically (at run time) with a "class loading" process.
Each class definition is in its own file. The class name MUST be used as the file name and the extension is .java. Capitalization counts!
Applications must have a main method (just like C/C++). The JVM will look to the main method as the starting point.
Compiling is a command in the DOS or telnet window such as
javac myclass.java
Running the program from the DOS or Unix command window is
java myclass
Output to console uses methods
System.out.print(string)
System.out.println(string)The strings may contain the standard escape sequences \n, \t, \\, \"
How is the Java runtime environment different from compiled languages? and also from interpreted languages?
What are the three phases to execute Java code?
What is a "Just-in-time" compiler?
Locality refers to the development of off-the-shelf components
Class files can be put into
Java lets us group related classes together into a unit called a package.
Packages provide several advantages:
A Java compilation unit can consist of a file with
The classes defined in the file are members of the package. The name of the file containing the compilation unit must match the name of the public class within the unit. Each Java compilation unit is stored in its own file.
The Java system identifies the file using a combination of the package name and the name of the public class in the compilation unit. Java restricts us to having a single public class in a file so that it can use file names to locate all public classes. Thus, a package with multiple public classes must be implemented with multiple compilation units, each in a separate file.
In order to access the contents of a package from within a program, you must import it into your program:
The Java package rules are defined to work seamlessly with hierarchical file systems:
import ch03.stacks.*;
What's shown is what the user needs. The details of implementation are hidden.
Strings are sequences of characters, but you don't really know how it is stored (info hiding); strings are objects.
Characters are represented internally as Unicode rather than ASCII (2 bytes).
Concatenation operation is +
Strings are immutable-- you cannot change a string. If you want to change a string you must create a new one.
Garbage collection is part of the JVM to recover the memory from old, discarded objects or strings.
| char charAt(int) | int compareTo(Str) | boolean equals(Str) | boolean equalsIgnoreCase(Str) | int indexOf(char) int indexOf(Str) in indexOf(Str,index) |
| int length(Str) | String replace(char,char) | String substring(index) String substring(beg,end) |
String toLowerCase(Str) String toUpperCase(Str) |
String trim() |