How to cast Double to Integer in Java

June 15, 2017

To cast Double (not-primitive) value to Integer we can use the intValue() method:


To cast double to int you can use the casting operator


You can find a detailed description below.


1. Object type

It is worth to say that we don’t need to create an object with the new operator when it comes to basic types. Java let us create them like primitive ones automatically converting them to Objects (and vice versa!). It is called autoboxing.

As you can see in the code we take the value from by intValue() method. We can change the type name in method name when we need to cast another type! (e.g. floatValue()).

Obviously code prints:

2. Primitive type

This time we don’t need methods for casting. It is the same like in C++ . We just write (int) before the variable name to cast.

The code prints:


Click here to add a comment

Leave a comment: