Java OO and Compilation

last updated 1/20/08

 


Classes and Objects

An object has behavior.

An object has state.

An object has a unique identity, even if it has the same state as another object

 

 

 


Modules and Information Hiding

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.

 

 

 


Views

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.

 


Advantages of separate compilation:

  1. One module may be used by many clients
  2. Individual modules may be modified and recompiled without recompiling the entire program
  3. Clients of a module may be modified and recompiled without recompiling that module.
  4. After a module's object file is generated, the source may be removed to hide the details

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.  

 


Compiling Java

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, \\, \"

 


The Java Language Environment

How is the Java runtime environment different from compiled languages? and also from interpreted languages?

What are the three phases to execute Java code?

  1. class loader -- load the class file containing the compiled bytecodes; reviews other referred class files and retrieves them, even from the Internet
  2. bytecode verifier -- enforce the security model that no illegal byte codes are found and that the codes and operations are also legal in the context of the environment.  E.g., a downloaded applet that accesses a database can only access the database that is on the same host that served the applet
  3. interpreter -- execute the bytecodes

What is a "Just-in-time" compiler?

 

 


Off -the-shelf components

Advantages

Locality

Locality refers to the development of off-the-shelf components

Java Packages

Class files can be put into


Packages

Java lets us group related classes together into a unit called a package.

Packages provide several advantages:

Using Packages

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.*;

 


Information Hiding

  1. The user is provided with only the information needed to use a module.
  2. The implementer is provided with only the information needed to implement a module.

Imports and exports

General Booch Diagram

What's shown is what the user needs.  The details of implementation are hidden.

 

 


String class in Java

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()