Number Class in Java

In this tutorial, we are going to learn about number class in Java and how different mathematical functions are used in Java followed by some handy examples.


What is Number class in Java?

Usually when we work with numbers in Java we use primitive data types like byte, integer, long, extra. All these primitive data types of numbers belong to a super class known as Number class. And all the direct known subclasses are byte, double, float, integer, short, long etc.

Look at the methods of the subclasses of the superclass Number:

xxxValue()

Converts the value of the the number of object that you provide into a desired data type that you want and return it.

Example:

public class Test { 

   public static void main(String args[]) {
      Integer a = 10;
      
      // Returns byte value of a
      System.out.println( a.byteValue() );

      // Returns float value of a
      System.out.println(a.floatValue());
   }
}

Output:

10
10.0

Recommended Books



compareTo()

Compare the value of the number object of a particular data type with the number that you provide within the parenthesis of the method.

Example:

public class Test { 

   public static void main(String args[]) {
      Integer a = 10;
      
      // If the value compared is greater than a then it's gonna return -1
      System.out.println( a.compareTo(11) );

      // If the value compared is less than a then it's gonna return 1
      System.out.println(a.compareTo(4));
      
      // If the value compared is equal to a then it's gonna return 0
      System.out.println(a.compareTo(10));
      
      
   }
}

Output:

-1
1
0




equals()

This method shows whether the number provided equal to the number that is present in the argument.

Example:

public class Test { 

   public static void main(String args[]) {
      Integer a = 10, b = 4, c =5;
      
      
      // If the value in the argument is equal to a then True will be returned
      System.out.println( a.equals(a));
      System.out.println(a.equals(b));
      System.out.println(a.equals(c));
      
      
   }
}

Output:

true
false
false

valueOf()

This method Returns a number object that is passed inside an argument. The argument should be a string or any other data type.

parseInt()

This method returns the primitive data type of a string that is passed inside the argument.

abs()

This method returns the absolute value that is past inside the argument.

round()

This method returns the round figure of the value that is provided inside the argument.

min()

This method takes two arguments but returns the smallest of the two numbers provided.

max()
This method also takes two arguments but returns the largest of the two numbers provided.

exp()

This method returns the e of the argument provided.

sqrt()

This method returns the square root of the argument provided.

sin()

this method returns the sine of the argument provided

cos()

this method returns the cosine of the argument provided.

tan()

this method returns the tangent of the argument provided.

toDegrees()

this method returns the degrees of the value that is provided as an argument.

toRadians()

this method returns the radians of the value that is provided as an argument.

random()

This method returns a random number.