Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I cast a double to an int in Java?

Tags:

java

I'm looking at http://docs.oracle.com/javase/6/docs/api/java/lang/Double.html

I am trying

    double b = Math.sqrt(absoluteNumber);
    int c = b.intValue();

but I am getting this error:

Factorise.java:13: error: double cannot be dereferenced
int c = b.intValue();

Help please?

like image 443
Eamon Moloney Avatar asked Nov 17 '12 17:11

Eamon Moloney


1 Answers

double is not an object, it is a primitive type.

Simply writing (int)b will do the job.

If you really need a Double object, you need to construct one.

like image 93
bmargulies Avatar answered Oct 13 '22 22:10

bmargulies