JADE distinguishes between object-references (references to objects that are instances of a class or interface) and primitive-values (instances of primitive types such as
The assignment of object references uses reference semantics. When one object reference is assigned to another, the object itself is not copied but instead both variables now refer to the same object, as shown in the following example.
vars fred, joe : Customer; begin create fred transient; fred.name := "Fred"; joe := fred; // joe shares a reference to fred joe.name := "Joe"; end;
In the previous example, as the variables joe and fred both refer to the same object, the statement joe.name := "Joe"; also changes the name of fred.
The assignment of primitive values uses value semantics. The actual value is copied to the target variable and is therefore completely independent of the original value, as shown in the following example.
vars int1, int2 : Integer; str1, str2 : String; begin int1 := 10; int2 := int1; // int2 gets a copy of int1 int1 := int1 + 1; ... str1 := "Box"; str2 := str1; // str2 gets a copy of str1 str1[1] := "F"; end;
In this example, when int1 is incremented by the instruction int1 := int1 + 1, int2 is unaffected, and retains the value 10. Similarly, when the first character of str1 is set to "F", str2 is unaffected, and retains the value of "Box". This distinction between reference and value semantics also applies to the copying of method parameters and return values.