Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java casting confusion

Tags:

java

casting

Could anyone please tell me why the following casting is resulting in compile time error:

Long l = (Long)Math.pow(5,2);

But why not the following:

long l = (long)Math.pow(5,2);
like image 819
Stardust Avatar asked Dec 07 '22 04:12

Stardust


1 Answers

Math.pow(5,2) is a double, which can be cast to long. It can not be cast to Long.

This would however work fine, thanks to autoboxing which converts between long and Long:

Long l = (long)Math.pow(5,2);

To summarize, you can convert double --> long and long --> Long, but not double --> Long

like image 153
Martin Avatar answered Dec 16 '22 07:12

Martin