2

My program has a method that fetches the count of a particular row and convert it into BigInteger:

private BigInteger getSameSatPremise(ServiceAgreement sa) {
BigInteger count = BigInteger.ZERO;

    StringBuffer queryHql = new StringBuffer();
    queryHql.append("from Table t1");
    Query query = createQuery(queryHql.toString());
    query.addResult("count", "count(distinct t1.column1)");
    if (query.listSize() > 0) {
        count = (BigInteger) query.firstRow();
    }
    return count;
}

The casting works fine when the result from the query is 0. But I get a casting error like below when the result from the query is 2.

Caused by: java.lang.ClassCastException: java.lang.Long cannot be cast to java.math.BigInteger

Can anyone help.

  • 3
    Why do you think you can cast that? Use `BigInteger.valueOf(query.firstRow())`. – Tom Apr 06 '16 at 11:33
  • 2
    Methinks this question is reasonable, especially if the OP comes from C++. – Bathsheba Apr 06 '16 at 11:39
  • 1
    Why are you trying to widen a `long`? If the column is a long type, why not use `long`? – Elliott Frisch Apr 06 '16 at 11:44
  • 1
    `casting works fine when the result from the query is 0` No. In that case `query.listSize() > 0` you don't cast. You return the initial value of `count` which is `BigInteger.ZERO`. – SubOptimal Apr 06 '16 at 11:52
  • 1
    @Tom query.firstRow() is a Object type. Maybe you'll have to do some research next time before answering. – Vishnu Priyan Rangasamy Apr 06 '16 at 12:53
  • 1
    Since your were inable to define which classes/libraries you're using, it was impossible to do research on that. That `query.firstRow()` returns `Object` instead of `Long` (or `long`) is crucial information and it _needs_ to be in that question. And please, learn the difference between "answering" and commenting. – Tom Apr 06 '16 at 13:01

3 Answers3

12

Java ain't C++, you can't write your own conversion operators.

In Java you need to use explicit functions to do what you want. In your case that's the considerably more verbose

BigInteger.valueOf(query.firstRow())

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

Long is not a subclass of BigInteger, so 'Long is NOT BigInteger'. So, your Long can not be cast to a BigInteger.

https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html

https://docs.oracle.com/javase/7/docs/api/java/lang/Long.html

Use static method of BigInteger: BigInteger.valueOf(long val); as BigInteger.valueOf(query.firstRow()).

And your code works in case of zero results because, you have initialized the count with a ZERO of type BigInteger. So, in case of zero results, your code does not get into the if statement (no casting is tried) and returns count straight away.

You might also want to read about Upcasting and Downcasting in Java.

What is the difference between up-casting and down-casting with respect to class variable

Community
  • 1
  • 1
Anmol Gupta
  • 2,797
  • 9
  • 27
  • 43
1

java.lang.Long and java.math.BigInteger are in a different hierarchies. They cannot be castes to each other. You can create new BigInteger by using static factory method:

BigInteger.valueOf(yourLong);
Rudziankoŭ
  • 10,681
  • 20
  • 92
  • 192