Interpretation of numeric literals without explicit type suffix
Activity
Show:
Steve Ebersole July 20, 2023 at 2:14 PM
See continuing discussion on

Gavin King June 19, 2023 at 9:55 PM
Since both Java and Hibernate 5 interpret 100.5
as type a Double
, I agree that Hibernate 6 should be changed to do the same.
This could be either a documentation or implementation issue.
Section “16.3.3. Numeric literals” of the documentation shows some examples of numeric literals, with and without explicit type suffixes:
“1” is indicated as a “simple integer literal”;
“1L” is a “simple integer literal, typed as a long”;
“100.5” is “decimal notation”;
“100.5F” is “decimal notation, typed as a float”;
“1e+2” is “scientific notation”;
“1e+2F” is “scientific notation, typed as a float”.
It also states that “It’s not usually necessary to specify the precision explicitly.”
The problem I have with this is that it does not state how literals are interpreted when the type suffix is not specified.
My initial reading was that “100.5” would be interpreted as a BigDecimal. This is because the term “decimal” itself is a bit ambiguous, and the example is distinguished from the next one, which is indicated as “typed as float”.
However, it seems that it is in fact interpreted as float. I ran into this with a JPQL query containing a literal value that cannot be exactly represented as a float:
SELECT b.isbn, b.title, b.score FROM Book b WHERE b.score = 199999.99
Which is translated to the following SQL:
select b1_0.ISBN_,b1_0.TITLE_,b1_0.SCORE_ from BOOK b1_0 where b1_0.SCORE_=199999.98
(Note that Book.score is a BigDecimal in the Java model class)
So IMO it would help if the documentation would explicitly state how such literals are interpreted. One might also wonder if float (rather than say, double) is a good default representation. In Java, according to the JLS Section 3.10.2, "Floating-Point Literals," a floating-point literal without a suffix is of type
double
by default. The JPA 3.1 spec states that “Approximate literals support the use Java floating point literal syntax as well as SQL approximate numeric literal syntax.” This would suggest to me thatdouble
should be the default for JPQL as well.