• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem casting a BigDecimal from a Long  RSS feed

 
Bill Clar
Ranch Hand
Posts: 163
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I'm getting an error: BigDecimal incompatible with java.lang.Long, when I try to run the following code:



Can a BigDecimal be cast to a Long in Java 7? Will it throw an error in Java 6?

FYI, this is legacy code.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, a Long and BigDecimal do not share any kind of "is a" relationship; so cannot be casted. Look at the JavaDoc for BigDecimal to see the proper path to take.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. What does getValue() return? Why convert it to long before trying to convert it to BigDecimal?
 
Bill Clar
Ranch Hand
Posts: 163
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's legacy code and it runs under my workspace but not my coworkers.

getValue() returns an Object.

It's legacy code. I don't know their intentions.

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill Clar wrote:getValue() returns an Object.

But what is it really returning? Obviously it cannot be an instance of Object.
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
Bill Clar wrote:getValue() returns an Object.

But what is it really returning? Obviously it cannot be an instance of Object.


Well, Object is a non-abstract class with a public constructor - it can instantiated ;-)

Anyway, Number could be a common denominator.

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!