A programmer coming from Java background into X++ programming owned by Microsoft will see a lot of similarities in terms of the concepts and syntax. However, there are few subtle differences that a programmer should be aware of. Arithmetic operators is one of the important areas when it comes to programming. By not understanding the operators and how they function, a developer could misuse them and results will not be the same as expected.
Use of the slash (/) operator
In Java, the / operator is used for integer division. For example, 5/3 will output 1, as shown below using a Java interactive console:
In order to do a decimal division, we will have to specify the numerator and denominator as real (float) numbers, as shown below in Java. Please note that the result is displayed with double precision, which shows more significant digits after the decimal point.
X++ uses the / operator little differently. It treats the operator as an actual real number division, as shown below. The beauty is that we do not need to cast the numerator or denominator as real numbers.
Sample code for various operators in X++ is shown below:
/// Runs the class with the specified arguments.
/// <param name = “_args”>The specified arguments.</param>
public static void main(Args _args)
info(strfmt(“5/3 yields %1”, 5/3));
info(strFmt(“~(4) is %1”, ~4));
info(strFmt(“2 ^ 5 is %1”, 2 ^ 5));
info(strFmt(“2 | 3 is %1”, 2 | 3));
info(strFmt(“2 & 3 is %1”, 2 & 3));
info(strFmt(“3 DIV 2 is %1”, 3 div 2));
info(strFmt(“Flowery is like Flowery %1”, “Flowery” like “Flowery”));
info(strFmt(“2 != 3 %1”, 2 != 3));
info(strFmt(“True && True %1”, True && True));
Output for the X++ operators is shown below:
There is another division operator in X++, DIV, which does not exist in Java. It is used for integer division. It is equivalent of / in Java for integer division. Therefore, 5 DIV 3 will produce output as 1. The following operators are the same in both programming languages:
|Operators||Java and X++|
|<<, >>, *, /, ~, &, ^, |, +, -, ?:, !, !=, &&, ||, <, ==,
|Same in both languages|
The following operators are not the same in the two languages:
|DIV||Does not exist. Equivalent is /||Exists|
|MOD||Does not Exists. Equivalent is %||Exists|
Stay tuned for next month’s blog, where I will continue with similarities and dissimilarities between Java and X++.
By: Altaf Siddiqui