Skip to content

Commit 9b5febe

Browse files
committed
Document SpEL limitations for minimum values for numeric literals
Closes gh-20779
1 parent 7627d8c commit 9b5febe

File tree

1 file changed

+24
-0
lines changed
  • framework-docs/modules/ROOT/pages/core/expressions/language-ref

1 file changed

+24
-0
lines changed

Diff for: framework-docs/modules/ROOT/pages/core/expressions/language-ref/literal.adoc

+24
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,30 @@ Boolean ::
2020
Null ::
2121
`null`
2222

23+
[NOTE]
24+
====
25+
Due to the design and implementation of the Spring Expression Language, literal numbers
26+
are always stored internally as positive numbers.
27+
28+
For example, `-2` is stored internally as a positive `2` which is then negated while
29+
evaluating the expression (by calculating the value of `0 - 2`).
30+
31+
This means that it is not possible to represent a negative literal number equal to the
32+
minimum value of that type of number in Java. For example, the minimum supported value
33+
for an `int` in Java is `Integer.MIN_VALUE` which has a value of `-2147483648`. However,
34+
if you include `-2147483648` in a SpEL expression, an exception will be thrown informing
35+
you that the value `2147483648` cannot be parsed as an `int` (because it exceeds the
36+
value of `Integer.MAX_VALUE` which is `2147483647`).
37+
38+
If you need to use the minimum value for a particular type of number within a SpEL
39+
expression, you can either reference the `MIN_VALUE` constant for the respective wrapper
40+
type (such as `Integer.MIN_VALUE`, `Long.MIN_VALUE`, etc.) or calculate the minimum
41+
value. For example, to use the minimum integer value:
42+
43+
- `T(Integer).MIN_VALUE` -- requires a `StandardEvaluationContext`
44+
- `-2^31` -- can be used with any type of `EvaluationContext`
45+
====
46+
2347
The following listing shows simple usage of literals. Typically, they are not used in
2448
isolation like this but, rather, as part of a more complex expression -- for example,
2549
using a literal on one side of a logical comparison operator or as an argument to a

0 commit comments

Comments
 (0)