Java Number intValue() Method



The Java Number intValue() method converts the value of the specified number into an int value. Here, we say a 'number' as it can be in any form − short, long, float, double.

An int value is a primitive data type that only stores integer values. It is a 32-bit signed two's complement integer, whose range lies between -231 and 231-1. However, the int keyword can also represent unsigned 32-bit integers ranging from 0 to 232-1, in Java SE 8 and further versions.

The default value and size of the int data type is 0 and 4 bytes respectively.

Note − This conversion may involve rounding or truncation of the given number.

Syntax

Following is the syntax for Java Number intValue() method

public abstract int intValue()

Parameters

This method does not accept any parameters.

Return Value

This method returns the int value after conversion.

Example

The following example shows the usage of Java Number intValue() method. A float value is assigned to a Float class reference variable. This value will be converted into an integer value using this method, which is invoked on the Float object.

public class NumberIntVal {
   public static void main(String[] args) {

      // get a number as float
      Float x = new Float(123456f);

      // print their value as int
      System.out.println("x as float:" + x + ", x as Integer:" + x.intValue());
   }
}

Let us compile and run the above program, this will produce the following result −

x as float:123456.0, x as Integer:123456

Example

In another example, we assign a double value to a Double class reference variable. This method will be invoked on the Double class object; and the value assigned to it will be converted into an integer value as the result.

public class NumberIntVal {
   public static void main(String[] args) {

      // get a number as double
      Double x = 5829.45d;
  
      // print their value as int
      System.out.println("x as long: " + x + ", x as Integer: " + x.intValue());
   }  
}

The output for the program above, after compiling and executing it, is as follows −

x as long: 5829.45, x as Integer: 5829

Example

Here, we are assigning a long value to a Long class reference variable. The method will be invoked on the Long class object that is created, and the value assigned to it will be converted into an integer value.

public class NumberIntVal {
   public static void main(String[] args) {

      // get a number as long
      Long x = (long) 792866;
  
      // print their value as int
      System.out.println("x as long: " + x + ", x as Integer: " + x.intValue());
   }  
}

Let us compile and run the program given, the output is displayed as follows −

x as long: 792866, x as Integer: 792866

Example

In this example, a byte value is assigned to the Byte class reference variable and an object is created. Then the method will be invoked on the Byte object, and the byte value will be converted into an integer value as the result.

public class NumberIntVal {
   public static void main(String[] args) {

      // get a number as byte
      Byte x = 66;
  
      // print their value as int
      System.out.println("x as byte: " + x + ", x as Integer: " + x.intValue());
   }  
}

Compile and run the program to obtain the output as follows −

x as byte: 66, x as Integer: 66
java_lang_number.htm
Advertisements