Autoboxing

Autoboxing options convert primitive types to the corresponding wrapper types, and conversely. Autoboxing is enabled by default but you can disable it. Some wrapper instances are cached for better performance. Autoboxing and value caching affect the working memory.

ILOG® Rule Language (IRL) supports autoboxing and auto-unboxing. This feature provides automatic conversion of data of primitive type to the corresponding wrapper type, for example in to java.lang.Integer, and conversely.

The Java primitive types are boolean, byte, char, double, float, int, long, and short. Autoboxing and -unboxing works for all these types.

For example, if you want to add an integer in a collection, you can write:

collection.add(12);

Autoboxing or auto-unboxing can occur in assignments, method or function arguments and return values, binary operators, unary operators (except ++ and --), casts, and insertion or retraction of objects in working memory.

Enabling and disabling autoboxing

Autoboxing is enabled by default in Decision Server. To disable autoboxing, you must set the property ilog.rules.engine.autoboxing to false in the engine configuration file (see Configuration API and properties).

Before you choose to disable autoboxing, consider the effect on the semantics of inserting wrapper type instances in the working memory, as described in Working memory and autoboxing.

Cache of values

Some wrapper instances are cached for better performance. For example, java.lang.Integer instances are cached for values between -128 and 127. If you write:

Object o1 = 10;
Object o2 = 10;

o1 == o2 returns true.

But if you write:

Object o1 = 1000;
Object o2 = 1000;

o1 == o2 returns false.

Working memory and autoboxing

The autoboxing option and the value caching feature change the way in which you insert objects into the working memory. For example, you can now write insert 4;. Therefore, the semantics of inserting wrapper type instances into working memory is also affected. Now the equality of such instances is based on the equals method.

So, if you write:

insert 1000;
insert 1000;

you have only one instance of java.lang.Integer in the working memory.

In this example, if you disable autoboxing, or work with an older version of Decision Server, you get two instances of java.lang.Integer in the working memory.