Search | Synergex.com | Contact Us | Resource Center Login | Home|
News

Synergy/DE pros, see if you can answer this question!

What is the output of the following program on the latest version of Synergy/DE?

main
record
         a     ,@d
         b     ,@d
proc
         open(1,o,"TT:")
         a = 1
         b = 1
         writes(1, string(a == b))
end
a. "true"
b. "false"
c. "1"
d. "0"
e. This does not compile

Correct answer: (d)

This program assigns the numeric value 1 to both variables a and b, then outputs the string representation of their comparison.  If a and b were decimal fields, the test for equality would yield a true value.  However, they are not decimal fields, but rather fields that reference a boxed decimal object instead.  Assigning a decimal value to one of these automatically boxes the value in an object of type @d.  Object equality compares the instances to see if the two expressions refer to the same object.  In this case, they refer to two different boxed decimals whose values happen to be the same.  Thus, they are not equal.

In Synergy/DE, Boolean (true/false) expressions are numeric values: 1 for true, and 0 for false.  Thus, the result of converting the expression "a == b" to a string yields "0" in this case.

This example highlights some of the differences between primitive data types and their boxed equivalents.  Boxing allows you to use a primitive type where only an object is allowed -- for instance, as an element in an ArrayList.  But boxing also adds a small overhead and a degree of clumsiness when accessing the value.  In the example above, if we had wanted to obtain a comparison of the values instead of the object instances, we would have needed to unbox them and rewrite the conditional as ((d)a == (d)b) instead.

More information about News and Events

Contact Synergex to learn more about  News and Events