I ran this code snippet in anonymous apex:

Integer slaM = 60000;

Long L1 = 60000 * slaM;

Long L2 = 60000 * (long) slaM;

system.debug ('L1: ' + L1 + ' /L2: ' + L2) 

Here is the debug output:

DEBUG|L1: -694967296 / L2: 3600000000

So when I multiply a long with a long, the result is correct. But when I multiply a long with an integer, and it reaches a certain size, it seems to be using the 2's complement of the number. Is this expected?

1 Answer 1


Yes, this is expected. Multiplying two Integer values (60000 is an Integer value, not a Long), results in an Integer (the upper 32-bits of the the result are truncated/lost), while multiplying an Integer by a Long causes the Integer to be automatically promoted to a Long, which calculates and outputs a Long result. You can force a literal long with the L suffix:

Integer slaM = 60000;
Long L1 = 60000L * slaM;
Long L2 = 60000 * (long) slaM;

Now, L1 and L2 will both have the same result, as the slaM in L1 is automatically promoted to a Long because 60000L is a Long value.

  • Thanks Fox, that makes perfect sense. What is strange is that this arose when I was calling BusinessHours.add method, which takes a Long as the final parameter. I was passing in "60000 * slaM" as that parameter. If, as you say, that is an integer, should it not have have resulted in a compile error, or at least a run time error?
    – hamayoun
    Jan 18, 2023 at 18:38
  • 1
    @hamayoun No, because of automatic promotion. The two Integer values are multiplied, an Integer is returned (the upper 32-bits are discarded), then that Integer is promoted to a Long because that's what the parameter data type is. That's just how programming works, unfortunately. In your case, though, the fix is just adding an L to your code :D
    – sfdcfox
    Jan 18, 2023 at 18:41
  • After 30+ years, I still need to learn Programming 101 ;-)
    – hamayoun
    Jan 18, 2023 at 18:43
  • If anyone is interested in the full story: stackoverflow.com/questions/46073295/…
    – hamayoun
    Jan 18, 2023 at 18:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .