Skip to content

Instantly share code, notes, and snippets.

@alan-morey
Created October 1, 2014 22:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alan-morey/c96ba665a49510160f08 to your computer and use it in GitHub Desktop.
Save alan-morey/c96ba665a49510160f08 to your computer and use it in GitHub Desktop.
Salesforce Apex bug with Long and Integer when trying to represent the minimum value as a literal
// Apex seems to have a bug when trying to represent the minimum value for both
// Long and Integer primitive types as a literal value.
//
//Long MIN_LONG = -9223372036854775808L; // Compile Error: Invalid Long: 9223372036854775808L
//Integer MIN_INT = -2147483648; // Compile Error: Invalid Integer: 2147483648
// Workaround: Overflow the MAX values
Long LONG_MAX_VALUE = 9223372036854775807L;
Long LONG_MIN_VALUE = LONG_MAX_VALUE + 1; // Overflow => -9223372036854775808L
Integer INT_MAX_VALUE = 2147483647;
Integer INT_MIN_VALUE = INT_MAX_VALUE + 1; // Overflow => -2147483648
// Alternative Workaround: Use string literal and convert with *.valueOf(String)
String LONG_MIN_AS_STRING = '-9223372036854775808';
String INT_MIN_AS_STRING = '-2147483648';
Long ALT_LONG_MIN_VALUE = Long.valueOf(LONG_MIN_AS_STRING);
Integer ALT_INT_MIN_VALUE = Integer.valueOf(INT_MIN_AS_STRING);
System.assertEquals(LONG_MIN_VALUE, ALT_LONG_MIN_VALUE);
System.assertEquals(INT_MIN_VALUE, ALT_INT_MIN_VALUE);
System.assertEquals(LONG_MIN_AS_STRING, String.valueOf(ALT_LONG_MIN_VALUE));
System.assertEquals(LONG_MIN_AS_STRING, String.valueOf(LONG_MIN_VALUE));
System.assertEquals(INT_MIN_AS_STRING, String.valueOf(ALT_INT_MIN_VALUE));
System.assertEquals(INT_MIN_AS_STRING, String.valueOf(INT_MIN_VALUE));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment