This article is series of Java Lang Package tutorial. In this tutorial, we will discuss the important and commonly used Number class methods with examples.

The

*java.lang.Number*class is the superclass of classes BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, and Short. The Subclasses of*Number*must provide methods to convert the represented numeric value to byte, double, float, int, long, and short.
Check out below Java Core API packages tutorials:

## Java Number Class Methods

*byte byteValue()*- Returns the value of the specified number as a byte, which may involve rounding or truncation.*abstract double doubleValue()*- Returns the value of the specified number as a double, which may involve rounding.*abstract float floatValue()*- Returns the value of the specified number as a float, which may involve rounding.*abstract int intValue()*- Returns the value of the specified number as an int, which may involve rounding or truncation.*abstract long longValue()*- Returns the value of the specified number as a long, which may involve rounding or truncation.*short shortValue()*- Returns the value of the specified number as a short, which may involve rounding or truncation.

## Java Number Class Methods/APIs with Examples

### byte byteValue()

This method returns the value of the specified number as a byte, which may involve rounding or truncation.

```
public void byteValueMethod() {
// get a number as integer
Number x = new Integer(123456);
// get a number as float
Number y = new Float(9876f);
// print their value as byte
System.out.println("x as integer:" + x + ", x as byte:" + x.byteValue());
System.out.println("y as float:" + y + ", y as byte:" + y.byteValue());
}
```

Output:

```
x as integer:123456, x as byte:64
y as float:9876.0, y as byte:-108
```

### abstract double doubleValue()

This method returns the value of the specified number as a double, which may involve rounding.

```
public void doubleValueMethod() {
// get a number as integer
Number x = new Integer(123456);
// get a number as float
Number y = new Float(9876f);
// print their value as double
System.out.println("x as integer :" + x + ", x as double:" + x.doubleValue());
System.out.println("y as float::" + y + ", y as double:" + y.doubleValue());
}
```

Output:

```
x as integer :123456, x as double:123456.0
y as float::9876.0, y as double:9876.0
```

### abstract float floatValue()

This method returns the value of the specified number as a float, which may involve rounding.

```
public void floatValueMethod() {
// get a number as integer
Number x = new Integer(123456);
// get a number as double
Number y = new Double(9876);
// print their value as float
System.out.println("x as integer:" + x + ", x as float:" + x.floatValue());
System.out.println("y as double:" + y + ", y as float:" + y.floatValue());
}
```

Output:

```
x as integer:123456, x as float:123456.0
y as double:9876.0, y as float:9876.0
```

### abstract int intValue()

This method returns the value of the specified number as an int, which may involve rounding or truncation.

```
public void intValueMethod() {
// get a number as float
Number x = new Float(123456f);
// get a number as double
Number y = new Double(9876);
// print their value as int
System.out.println("x as float:" + x + ", x as Integer:" + x.intValue());
System.out.println("y as double:" + y + ", y as Integer:" + y.intValue());
}
```

Output:

```
x as float:123456.0, x as Integer:123456
y as double:9876.0, y as Integer:9876
```

### abstract long longValue()

This method returns the value of the specified number as a long, which may involve rounding or truncation.

```
public void longValueMethod() {
// get a number as float
Number x = new Float(123456f);
// get a number as double
Number y = new Double(9876);
// print their value as long
System.out.println("x as float:" + x + ", x as long:" + x.longValue());
System.out.println("y as double:" + y + ", y as long:" + y.longValue());
}
```

Output:

```
x as float:123456.0, x as long:123456
y as double:9876.0, y as long:9876
```

### short shortValue()

This method returns the value of the specified number as a short, which may involve rounding or truncation.

```
public void shortValueMethod() {
// get a number as float
Number x = new Float(123456f);
// get a number as double
Number y = new Double(9876);
// print their value as short
System.out.println("x as float :" + x + ", x as short:" + x.shortValue());
System.out.println("y as double:" + y + ", y as short:" + y.shortValue());
}
```

Output:

```
x as float :123456.0, x as short:-7616
y as double:9876.0, y as short:9876
```

## Complete Example

```
package net.javaguides.lang;
/**
* Class demonstrates the usage of Random class methods with examples
*
* @author Ramesh Fadatare
*
*/
public class NumberClassMethods {
public static void main(String[] args) {
NumberClassMethods classMethods = new NumberClassMethods();
classMethods.shortValueMethod();
classMethods.longValueMethod();
classMethods.intValueMethod();
classMethods.floatValueMethod();
classMethods.doubleValueMethod();
classMethods.byteValueMethod();
}
public void shortValueMethod() {
// get a number as float
Number x = new Float(123456 f);
// get a number as double
Number y = new Double(9876);
// print their value as short
System.out.println("x as float :" + x + ", x as short:" + x.shortValue());
System.out.println("y as double:" + y + ", y as short:" + y.shortValue());
}
public void longValueMethod() {
// get a number as float
Number x = new Float(123456 f);
// get a number as double
Number y = new Double(9876);
// print their value as long
System.out.println("x as float:" + x + ", x as long:" + x.longValue());
System.out.println("y as double:" + y + ", y as long:" + y.longValue());
}
public void intValueMethod() {
// get a number as float
Number x = new Float(123456 f);
// get a number as double
Number y = new Double(9876);
// print their value as int
System.out.println("x as float:" + x + ", x as Integer:" + x.intValue());
System.out.println("y as double:" + y + ", y as Integer:" + y.intValue());
}
public void floatValueMethod() {
// get a number as integer
Number x = new Integer(123456);
// get a number as double
Number y = new Double(9876);
// print their value as float
System.out.println("x as integer:" + x + ", x as float:" + x.floatValue());
System.out.println("y as double:" + y + ", y as float:" + y.floatValue());
}
public void doubleValueMethod() {
// get a number as integer
Number x = new Integer(123456);
// get a number as float
Number y = new Float(9876 f);
// print their value as double
System.out.println("x as integer :" + x + ", x as double:" + x.doubleValue());
System.out.println("y as float::" + y + ", y as double:" + y.doubleValue());
}
public void byteValueMethod() {
// get a number as integer
Number x = new Integer(123456);
// get a number as float
Number y = new Float(9876 f);
// print their value as byte
System.out.println("x as integer:" + x + ", x as byte:" + x.byteValue());
System.out.println("y as float:" + y + ", y as byte:" + y.byteValue());
}
}
```

Output:

```
x as float :123456.0, x as short:-7616
y as double:9876.0, y as short:9876
x as float:123456.0, x as long:123456
y as double:9876.0, y as long:9876
x as float:123456.0, x as Integer:123456
y as double:9876.0, y as Integer:9876
x as integer:123456, x as float:123456.0
y as double:9876.0, y as float:9876.0
x as integer :123456, x as double:123456.0
y as float::9876.0, y as double:9876.0
x as integer:123456, x as byte:64
y as float:9876.0, y as byte:-108
```

## Comments

## Post a Comment

Leave Comment