ConstJava

ConstJava is an extension of the Java language that permits the specification and compile-time verification of immutability constraints.

IMPORTANT: ConstJava has been obsoleted by the newer Javari language; these webpages are retained only for historical interest.


Motivation

An important goal in language design is making it easier for the programmer to specify constraints on code. Such constraints can ease or accelerate the detection of errors and hence reduce the time spent debugging. An example of such a constraint is static type-checking, which is now found in most languages. Another such constraint is the ability to specify that an object is immutable, or another version of such constraint, that it cannot be changed through a given reference.

The Java language lacks the ability to specify immutability constraints. ConstJava is an extension to the Java language that permits the specification and compile-time verification of immutability constraints. ConstJava specifies immutability constraints using the keyword const, whose usage is modeled after C++. The language is backwards compatible with Java. In addition, ConstJava code is interoperable with Java code, and runs on an unmodified Java Virtual Machine.

ConstJava permits the specification of the following constraint: the transitive state of the object to which a given reference refers cannot be modified using that reference. ConstJava does not place any guarantee on object immutability. However, if only constant references to a given object exist (a constant reference is one through which an object cannot be mutated), then the object is immutable. In particular, if at instantiation an object is assigned to a constant reference, the object is immutable.

Unlike other proposals for immutability specification, ConstJava provides useful guarantees even about code that manipulates mutable objects. For example, a method that receives a constant reference as a parameter will not modify that parameter, unless the parameter is aliased in a global variable or another parameter. This allows one to specify compiler-verified constraints on behaviour of methods, and eases reasoning about and optimization of programs.


The ConstJava language

The ConstJava language extends Java with explicit mechanisms for specifying immutability constraints and compile-time type-checking to guarantee those constraints. The syntax of the extensions is based on that of C++.

ConstJava adds four new keywords to Java: const, mutable, template, and const_cast. The first of these is the keyword used to specify immutability constraints. The other three are additional language features that, while not essential, make ConstJava a more useful language.

The keywords are used as follows:

ConstJava is backward compatible with Java: any Java program that uses none of ConstJava's keywords is a valid ConstJava program. Also, ConstJava is interoperable with Java. Any Java code can be called from ConstJava code.

In addition, a special comment syntax allows every ConstJava program to remain a valid Java program. Any comment that begins with /*= is considered as part of the code by ConstJava. This feature allows the programmer to annotate an existing Java program with const without losing the ability to compile that program with Java.

Differences from Javari

ConstJava has been obsoleted by the newer Javari language. Both languages enforce reference immutability. ConstJava differs from Javari (the language described in the OOPSLA 2005 paper) in several respects; Javari is more expressive and compatible with Java. ConstJava has the same semantics as Javari 2004 (the language described in the OOPSLA 2004 paper), but its implementation differs in the following ways:


References

IMPORTANT: ConstJava has been obsoleted by the newer Javari language; these webpages are retained only for historical interest. In particular, you should use the Javari documents and implementation rather than these.