sztgeza wrote: ↑Sat Sep 13, 2014 1:31 pm
The first correct answer (by the solution) is:
A field of type Integer and an input parameter of type double.
Exact (e.g. int) and approximate (e.g. double) numeric values can be compared.
However:
Code: Select all
Query q = em.createQuery("SELECT i from Item i where i.id < :p_id");
q.setParameter("p_id",7.0d);
List result = q.getResultList();
throws an IllegalArgumentException:
java.lang.IllegalArgumentException: Parameter value [7.0] did not match expected type [java.lang.Integer (n/a)]
Please, could you describe in more details, how can the id (Integer) and the double parameter be compared?
Quite simply I think what is happening here is that the usual Java SE rules for comparison are in place. You can observe this from the answers to the question.
Note that Java will unbox and widen but it will not widen and box.
So you can compare an Integer to a double, because it will unbox the Integer to int and then widen it to double:
Code: Select all
public static void main(String[] args) {
foo(2,2.2);
}
static void foo(Integer i, double d) {
if(i==d) {
System.out.println("true");
} else {
System.out.println("false");
}
}
The same thing happens with Float and double: it unboxes Float to float and widens it to double.