Easy Tutorial
❮ String Removing Char Java Hashmap Foreach ❯

Java Number & Math Classes

Generally, when we need to use numbers, we typically use built-in data types such as byte, int, long, double, etc.

Example

int a = 5000;
float b = 13.65f;
byte c = 0x4a;

However, in practical development, we often encounter situations where we need to use objects instead of built-in data types. To address this, the Java language provides wrapper classes for each built-in data type.

All wrapper classes (Integer, Long, Byte, Double, Float, Short) are subclasses of the abstract class Number.

Wrapper Class Primitive Data Type
Boolean boolean
Byte byte
Short short
Integer int
Long long
Character char
Float float
Double double

This special support by the compiler for wrapping is called boxing, so when built-in data types are used as objects, the compiler boxes the built-in type into a wrapper class. Similarly, the compiler can also unbox an object into a built-in type. The Number class belongs to the java.lang package.

Below is an example using the Integer object:

Test.java File Code:

public class Test{

   public static void main(String[] args){
      Integer x = 5;
      x =  x + 10;
      System.out.println(x); 
   }
}

The above example compiles and runs with the following result:

15

When x is assigned an integer value, since x is an object, the compiler needs to box x. Then, to allow x to perform addition, the compiler needs to unbox x.


Java Math Class

Java's Math class includes properties and methods for performing basic mathematical operations, such as elementary exponential, logarithmic, square root, and trigonometric functions.

The methods in Math are defined as static and can be directly called in the main function via the Math class.

Test.java File Code:

public class Test {  
    public static void main (String []args)  
    {  
        System.out.println("Sine of 90 degrees: " + Math.sin(Math.PI/2));  
        System.out.println("Cosine of 0 degrees: " + Math.cos(0));  
        System.out.println("Tangent of 60 degrees: " + Math.tan(Math.PI/3));  
        System.out.println("Arctangent of 1: " + Math.atan(1));  
        System.out.println("Degrees of π/2: " + Math.toDegrees(Math.PI/2));  
        System.out.println(Math.PI);  
    }  
}

The above example compiles and runs with the following result:

Sine of 90 degrees: 1.0
Cosine of 0 degrees: 1.0
Tangent of 60 degrees: 1.7320508075688767
Arctangent of 1: 0.7853981633974483
Degrees of π/2: 90.0
3.141592653589793

Number & Math Class Methods

The table below lists some commonly used methods of the Number & Math classes:

No. Method & Description
1 xxxValue() <br>Converts the Number object to the xxx data type and returns it.
2 compareTo() <br>Compares the number object with the argument.
3 equals() <br>Determines if the number object is equal to the argument.
4 valueOf() <br>Returns a Number object holding the specified built-in data type.
5 toString() <br>Returns the value as a string.
6 parseInt() <br>Parses the string as an int type.
7 abs() <br>Returns the absolute value of the parameter.
8 ceil() <br>Returns the smallest integer greater than or equal to the given parameter, with double precision.
9 floor() <br>Returns the largest integer less than or equal to the given parameter.
10 rint() <br>Returns the closest integer to the parameter, with double return type.
11 round() <br>It represents rounding, with the algorithm Math.floor(x+0.5), which adds 0.5 to the original number and then rounds down, so Math.round(11.5) results in 12, and Math.round(-11.5) results in -11.
12 min() <br>Returns the minimum of two parameters.
13 max() <br>Returns the maximum of two parameters.
14 exp() <br>Returns the natural number base e raised to the power of the parameter.
15 log() <br>Returns the natural logarithm of the parameter.
16 pow() <br>Returns the first parameter raised to the power of the second parameter.
17 sqrt() <br>Returns the square root of the parameter.
18 sin() <br>Returns the sine of the specified double parameter.
19 cos() <br>Returns the cosine of the specified double parameter.
20 tan() <br>Returns the tangent of the specified double parameter.
21 asin() <br>Returns the arcsine of the specified double parameter.
22 acos() <br>Returns the arccosine of the specified double parameter.
23 atan() <br>Returns the arctangent of the specified double parameter.
24 atan2() <br>Converts Cartesian coordinates to polar coordinates and returns the angle of the polar coordinates.
25 toDegrees() <br>Converts the parameter to degrees.
26 toRadians() <br>Converts degrees to radians.
27 random() <br>Returns a random number.

Comparison of Math's floor, round, and ceil Methods

Parameter Math.floor Math.round Math.ceil
1.4 1 1 2
1.5 1 2 2
1.6 1 2 2
-1.4 -2 -1 -1
-1.5 -2 -1 -1
-1.6 -2 -2 -1

floor, round, and ceil Examples:

public class Main {   
  public static void main(String[] args) {   
    double[] nums = { 1.4, 1.5, 1.6, -1.4, -1.5, -1.6 };   
    for (double num : nums) {   
      test(num);   
    }   
  }   

  private static void test(double num) {   
    System.out.println("Math.floor(" + num + ")=" + Math.floor(num));   
    System.out.println("Math.round(" + num + ")=" + Math.round(num));   
System.out.println("Math.ceil(" + num + ")=" + Math.ceil(num));
}
}

The above example outputs the following results when executed:

Math.floor(1.4)=1.0
Math.round(1.4)=1
Math.ceil(1.4)=2.0
Math.floor(1.5)=1.0
Math.round(1.5)=2
Math.ceil(1.5)=2.0
Math.floor(1.6)=1.0
Math.round(1.6)=2
Math.ceil(1.6)=2.0
Math.floor(-1.4)=-2.0
Math.round(-1.4)=-1
Math.ceil(-1.4)=-1.0
Math.floor(-1.5)=-2.0
Math.round(-1.5)=-1
Math.ceil(-1.5)=-1.0
Math.floor(-1.6)=-2.0
Math.round(-1.6)=-2
Math.ceil(-1.6)=-1.0
❮ String Removing Char Java Hashmap Foreach ❯